<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Julien Roche on Medium]]></title>
        <description><![CDATA[Stories by Julien Roche on Medium]]></description>
        <link>https://medium.com/@rochejul?source=rss-67bfe57c1d45------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*WLy9_FlPWnZ2_VF8.jpg</url>
            <title>Stories by Julien Roche on Medium</title>
            <link>https://medium.com/@rochejul?source=rss-67bfe57c1d45------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 05 Dec 2026 16:36:57 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@rochejul/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Connect HTML elements with only CSS — part 3 (use a bit of JS to be more generic)]]></title>
            <link>https://medium.com/@rochejul/connect-html-elements-with-only-css-part-3-use-a-bit-of-js-to-be-more-generic-ac4c8c519f87?source=rss-67bfe57c1d45------2</link>
            <guid isPermaLink="false">https://medium.com/p/ac4c8c519f87</guid>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[css]]></category>
            <dc:creator><![CDATA[Julien Roche]]></dc:creator>
            <pubDate>Sat, 31 Oct 2026 13:36:44 GMT</pubDate>
            <atom:updated>2026-10-31T13:36:44.525Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/840/1*63NS4kW1F5K_RwkVDt8nKw.png" /><figcaption>How could we connect HTML elements like a classical graph?</figcaption></figure><p>I recall the days where providing a complex UI, such as charts or UML diagrams.</p><p>Some solutions use SVG, such as <a href="https://www.jointjs.com/demos/force-directed-layout">JointJs</a>.</p><p>Some solutions use HTML Canvas.</p><p>Did we have another kind of solutions? When we look the current HTML/CSS state, couldn’t we do something with only CSS and very few JavaScript?</p><p>I made three articles to talk about some findings, using only CSS as much as possible and using if necessary (depending of what you want to do) some (minor) JavaScript.</p><p>On <a href="https://medium.com/@rochejul/connect-html-elements-with-only-css-part-2-automatically-position-with-only-css-21152c1c5998">the previous article</a>, we were focused on the way to adapt the link depending of the boxes positions between each other.</p><p>Now let’s see how could we generify the approach.</p><p>You could find <a href="https://github.com/rochejul/medium-connect-elements-with-css-part3">here </a>the repository with the referenced code.</p><p>First, we will generify our JavaScript.</p><p>Indeed, we did a direct link to the moveabble box:</p><pre>const box1Element = document.querySelector(&#39;.box--one&#39;);</pre><p>Instead, we will keep the moving element in memory only when we need it:</p><pre>let lastCursorPositionForBoxMovement = null;<br>let lastMovingBoxPosition = null;<br>let movingBoxElement;<br><br>const bodyElement = document.querySelector(&#39;.app__body&#39;);<br><br>bodyElement.addEventListener(&#39;pointermove&#39;, (event) =&gt; {<br>  console.log(&#39;Pointer moved in %s %s&#39;, event.offsetX, event.offsetY);<br><br>  if (lastMovingBoxPosition) {<br>    console.info(&#39;We compute the box position&#39;);<br>    const diffX = Math.round(<br>      event.clientX - lastCursorPositionForBoxMovement.x,<br>    );<br>    const diffY = Math.round(<br>      event.clientY - lastCursorPositionForBoxMovement.y,<br>    );<br><br>    console.log(&#39;The box has moved from %s %s&#39;, diffX, diffY);<br><br>    movingBoxElement.style = `left: ${lastMovingBoxPosition.x + diffX}px; top: ${lastMovingBoxPosition.y + diffY}px;`;<br>  }<br>});<br><br>bodyElement.addEventListener(&#39;pointerdown&#39;, (event) =&gt; {<br>  if (<br>    !lastMovingBoxPosition &amp;&amp;<br>    event.target.classList.contains(&#39;box--movable&#39;)<br>  ) {<br>    movingBoxElement = event.target;<br><br>    console.info(<br>      &#39;We will start to move the box from pointer position %s %s&#39;,<br>      event.clientX,<br>      event.clientY,<br>    );<br><br>    lastCursorPositionForBoxMovement = {<br>      x: event.clientX,<br>      y: event.clientY,<br>    };<br><br>    lastMovingBoxPosition = getBoxPosition(movingBoxElement);<br>    movingBoxElement.classList.add(&#39;box--move-in-progress&#39;);<br>  }<br>});<br><br>bodyElement.addEventListener(&#39;pointerup&#39;, () =&gt; {<br>  if (lastMovingBoxPosition) {<br>    console.info(&#39;We stop to move the box&#39;);<br>    lastCursorPositionForBoxMovement = null;<br>    lastMovingBoxPosition = null;<br>    movingBoxElement.classList.remove(&#39;box--move-in-progress&#39;);<br>    movingBoxElement = null;<br>  }<br>  });</pre><p>Let’s generify our CSS to not use direclty the CSS var related to our moveable box and static box.</p><p>We will use <a href="https://medium.com/@rochejul/css-mixin-is-not-yet-available-any-fallback-ccc85848e421">the fallback mechanism when CSS mixin are not available</a> and inject in the HTML scope the CSS var to use:</p><pre>&lt;div<br>    class=&quot;link&quot;<br>    style=&quot;--moving-element: --box1; --static-element: --box2&quot;<br>  &gt;&lt;/div&gt;</pre><p>And so in our CSS:</p><pre>.link {<br> /* ... */<br><br>  /* -------- */<br>  /* Default position */<br>  inset-block-start: anchor(var(--moving-element) center);<br>  inset-inline-start: anchor(var(--moving-element) center);<br>  inset-inline-end: anchor(var(--static-element) center);<br>  inset-block-end: anchor(var(--static-element) center);<br><br>  /* ... */<br>}</pre><p>On <a href="https://medium.com/@rochejul/connect-html-elements-with-only-css-part-2-automatically-position-with-only-css-21152c1c5998">the previous article</a>, we saw the @position-try rules are not nested compatible unlike @container rules, then we have to move the CSS we declared from the @position-try rules to some CSS sub-classes (we also here merge with the @container rules but we could keep them if we want):</p><pre>.link {<br> /* ... */<br><br> &amp;.link--left-top {<br>    /*Moving box on left top of static box*/<br>    inset-block-start: anchor(var(--moving-element) center);<br>    inset-inline-start: anchor(var(--moving-element) center);<br>    inset-inline-end: anchor(var(--static-element) center);<br>    inset-block-end: anchor(var(--static-element) center);<br>  }<br><br>  &amp;.link--left-bottom {<br>    /*Moving box on left bottom of static box*/<br>    background-position:<br>      center,<br>      0 100%,<br>      100% 0;<br>    inset-block-start: anchor(var(--static-element) center);<br>    inset-inline-start: anchor(var(--moving-element) center);<br>    inset-inline-end: anchor(var(--static-element) center);<br>    inset-block-end: anchor(var(--moving-element) center);<br>  }<br><br>  &amp;.link--right-top {<br>    /*Moving box on right top of static box*/<br>    background-position:<br>      center,<br>      0 100%,<br>      100% 0;<br><br>    inset-block-start: anchor(var(--moving-element) center);<br>    inset-inline-start: anchor(var(--static-element) center);<br>    inset-block-end: anchor(var(--static-element) center);<br>    inset-inline-end: anchor(var(--moving-element) center);<br>  }<br><br>  &amp;.link--right-bottom {<br>    /*Moving box on right bottom of static box*/<br>    inset-block-start: anchor(var(--static-element) center);<br>    inset-inline-start: anchor(var(--static-element) center);<br>    inset-inline-end: anchor(var(--moving-element) center);<br>    inset-block-end: anchor(var(--moving-element) center);<br>  }<br><br>  &amp;.link--on-above {<br>    background-image:<br>      linear-gradient(#000, #000), linear-gradient(to right, #000, #000),<br>      linear-gradient(#000, #000);<br>    background-position:<br>      0 0,<br>      center,<br>      100% 100%;<br>    background-size:<br>      2px 50%,<br>      100% 2px,<br>      2px 50%;<br>    inset-block-start: anchor(var(--moving-element) center);<br>    inset-inline-start: anchor(var(--moving-element) center);<br>    inset-inline-end: anchor(var(--static-element) center);<br>    inset-block-end: anchor(var(--static-element) center);<br>  }<br>}</pre><p>The last step: replace the @position-try mechanism with some JavaScript.</p><p>We need first a method to know where is the moveable box from the static box:</p><pre>function getMovingElementPositionToStaticElement(movingElement, staticElement) {<br>  const box1Position = getBoxPosition(movingElement);<br>  const box2Position = getBoxPosition(staticElement);<br><br>  const left =<br>    movingElement.clientWidth / 2 + box1Position.x &lt;<br>    box2Position.x + staticElement.clientWidth / 2;<br>  const top =<br>    movingElement.clientHeight / 2 + box1Position.y &lt;<br>    box2Position.y + staticElement.clientHeight / 2;<br><br>  const above =<br>    box2Position.x - staticElement.clientWidth &lt;<br>      box1Position.x + movingElement.clientWidth &amp;&amp;<br>    box1Position.x + movingElement.clientWidth &lt;<br>      box2Position.x + staticElement.clientWidth * 2 &amp;&amp;<br>    box1Position.y + movingElement.clientHeight &lt; box2Position.y;<br><br>  const below =<br>    box2Position.x - staticElement.clientWidth &lt;<br>      box1Position.x + movingElement.clientWidth &amp;&amp;<br>    box1Position.x + movingElement.clientWidth &lt;<br>      box2Position.x + staticElement.clientWidth * 2 &amp;&amp;<br>    box2Position.y + staticElement.clientHeight &lt;<br>      box1Position.y + movingElement.clientHeight;<br><br>  return { left, top, above, below };<br>}</pre><p>We also need some methods to inject the appropriate CSS class depending of the moveable position:</p><pre>function updateLinkClassSet(linkElement, { left, top }) {<br>  linkElement.classList.remove(<br>    &#39;link--left-top&#39;,<br>    &#39;link--left-bottom&#39;,<br>    &#39;link--right-top&#39;,<br>    &#39;link--right-bottom&#39;,<br>  );<br><br>  if (left &amp;&amp; top) {<br>    linkElement.classList.add(&#39;link--left-top&#39;);<br>  } else if (left &amp;&amp; !top) {<br>    linkElement.classList.add(&#39;link--left-bottom&#39;);<br>  } else if (!left &amp;&amp; top) {<br>    linkElement.classList.add(&#39;link--right-top&#39;);<br>  } else if (!left &amp;&amp; !top) {<br>    linkElement.classList.add(&#39;link--right-bottom&#39;);<br>  }<br>}<br><br>function updateLink(linkElement, movingElement, staticElement) {<br>  const position = getMovingElementPositionToStaticElement(<br>    movingElement,<br>    staticElement,<br>  );<br><br>  updateLinkClassSet(linkElement, position);<br>}</pre><p>Obviously, the <strong>updateLink</strong> method should be called when we move the moveable box, but also when we load the page:</p><pre>const staticElement = document.querySelector(&#39;.box--second&#39;);<br>const movableBoxElements = document.querySelectorAll(&#39;.box--movable&#39;);<br>const linkElements = document.querySelectorAll(&#39;.link&#39;);<br>const elementsMapping = new Map();<br><br>movableBoxElements.forEach((boxElement, index) =&gt; {<br>  elementsMapping.set(boxElement, {<br>    box: boxElement,<br>    link: linkElements[index],<br>  });<br><br>  updateLink(linkElements[index], boxElement, staticElement);<br>});</pre><p>If we decide to have two moveable box:</p><pre>&lt;div<br>  class=&quot;link&quot;<br>  style=&quot;--moving-element: --box1; --static-element: --box2&quot;<br>&gt;&lt;/div&gt;<br>&lt;div<br>  class=&quot;link&quot;<br>  style=&quot;--moving-element: --box3; --static-element: --box2&quot;<br>&gt;&lt;/div&gt;</pre><p>We will see that what we have done works smoothly:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/800/1*vzQQ9vosPdcjgQy09dFHxw.gif" /></figure><p>At the end we have seen CSS provide some way to do this kind of feature without full JavaScript or using SVG (or both).</p><p>Depending of what you need, you could be full CSS oriented and use a small piece of JavaScript.</p><p>Also keep in mind when @position-try rules will be nested compatible, it will be much more easier.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ac4c8c519f87" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Connect HTML elements with only CSS — part 2 (automatically position with only CSS)]]></title>
            <link>https://medium.com/@rochejul/connect-html-elements-with-only-css-part-2-automatically-position-with-only-css-21152c1c5998?source=rss-67bfe57c1d45------2</link>
            <guid isPermaLink="false">https://medium.com/p/21152c1c5998</guid>
            <category><![CDATA[css]]></category>
            <dc:creator><![CDATA[Julien Roche]]></dc:creator>
            <pubDate>Sat, 31 Oct 2026 13:36:04 GMT</pubDate>
            <atom:updated>2026-10-31T13:36:04.699Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/840/1*63NS4kW1F5K_RwkVDt8nKw.png" /><figcaption>How could we connect HTML elements like a classical graph?</figcaption></figure><p>I recall the days where providing a complex UI, such as charts or UML diagrams.</p><p>Some solutions use SVG, such as <a href="https://www.jointjs.com/demos/force-directed-layout">JointJs</a>.</p><p>Some solutions use HTML Canvas.</p><p>Did we have another kind of solutions? When we look the current HTML/CSS state, couldn’t we do something with only CSS and very few JavaScript?</p><p>I made three articles to talk about some findings, using only CSS as much as possible and using if necessary (depending of what you want to do) some (minor) JavaScript.</p><p>On<a href="https://medium.com/@rochejul/connect-html-elements-with-only-css-part-1-design-the-link-bcad92592d39"> the previous article</a>, we were focused on the way to design a squigglink link between boxes.</p><p>Now let’s be focused on the way to adapt the link depending of the boxes positions between each other.</p><p>You could find <a href="https://github.com/rochejul/medium-connect-elements-with-css-part2">here</a> the repository with the referenced code.</p><p>First step: let’s define our boxes. We will reuse the same pattern from the previous article.</p><p>On HTML side:</p><pre>&lt;section class=&quot;app__body&quot;&gt;<br>  &lt;article class=&quot;box box--one&quot;&gt;&lt;/article&gt;<br>  &lt;article class=&quot;box box--second&quot;&gt;&lt;/article&gt;<br>&lt;/section&gt;</pre><p>On CSS side:</p><pre>.box {<br>  box-shadow: 10px 10px 20px 5px rgba(0, 0, 0, 0.5);<br>  border: 1px solid transparent;<br>  border-radius: 1rem;<br>  pointer-events: none;<br>  position: absolute;<br>  height: 5rem;<br>  width: 5rem;<br>  z-index: 10;<br><br>  &amp;.box--one {<br>    anchor-name: --box1;<br>    background-color: lightgray;<br>    top: 25px;<br>    left: 55px;<br>  }<br><br>  &amp;.box--second {<br>    anchor-name: --box2;<br>    background-color: lightgreen;<br>    top: 185px;<br>    left: 505px;<br>  }<br>}<br><br>.link {<br>  background-repeat: no-repeat;<br>  min-block-size: 2px;<br>  position: absolute;<br>  z-index: 9;<br><br>  &amp;.link--debug {<br>    border: 1px dotted black;<br>  }<br>}</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*9yKYD1HyrCTrn8_JpTugoA.png" /><figcaption>Basic</figcaption></figure><p>Now it’s time to add some JavaScript for the movable aspect.</p><p>We will adapt our CSS and HTML to inform what is movable:</p><pre>&lt;section class=&quot;app__body&quot;&gt;<br>  &lt;article class=&quot;box box--movable box--one&quot;&gt;&lt;/article&gt;<br>  &lt;article class=&quot;box box--second&quot;&gt;&lt;/article&gt;<br>&lt;/section&gt;</pre><pre>.box {<br>  /* remove on the pointer-events usage */<br><br>  &amp;.box--movable {<br>    cursor: pointer;<br><br>    &amp;.box--move-in-progress {<br>      cursor: move;<br>    }<br>  }<br><br>  &amp;:not(.box--movable) {<br>    pointer-events: none;<br>  }<br>}</pre><p>Now let’s focus on the JavaScript. We need first a function to get the coordinates of a box:</p><pre>function getBoxPosition(element) {<br>  const computedStyle = element.computedStyleMap();<br><br>  return {<br>    x: computedStyle.get(&#39;left&#39;).value,<br>    y: computedStyle.get(&#39;top&#39;).value,<br>  };<br>}</pre><p>Then, we will move the movable box when the pointer is pressed and with an handler on the body of the page, we will detect the position of the movable box with the second box:</p><pre> let lastCursorPositionForBoxMovement = null;<br>  let lastBox1Position = null;<br><br>  const bodyElement = document.querySelector(&#39;.app__body&#39;);<br>  const box1Element = document.querySelector(&#39;.box--one&#39;);<br><br>  bodyElement.addEventListener(&#39;pointermove&#39;, (event) =&gt; {<br>    console.log(&#39;Pointer moved in %s %s&#39;, event.offsetX, event.offsetY);<br><br>    if (lastBox1Position) {<br>      console.info(&#39;We compute the box position&#39;);<br>      const diffX = Math.round(<br>        event.clientX - lastCursorPositionForBoxMovement.x,<br>      );<br>      const diffY = Math.round(<br>        event.clientY - lastCursorPositionForBoxMovement.y,<br>      );<br><br>      console.log(&#39;The box has moved from %s %s&#39;, diffX, diffY);<br><br>      box1Element.style = `left: ${lastBox1Position.x + diffX}px; top: ${lastBox1Position.y + diffY}px;`;<br>    }<br>  });<br><br>  box1Element.addEventListener(&#39;pointerdown&#39;, (event) =&gt; {<br>    console.info(<br>      &#39;We will start to move the box from pointer position %s %s&#39;,<br>      event.clientX,<br>      event.clientY,<br>    );<br><br>    lastCursorPositionForBoxMovement = {<br>      x: event.clientX,<br>      y: event.clientY,<br>    };<br><br>    lastBox1Position = getBoxPosition(box1Element);<br>    box1Element.classList.add(&#39;box--move-in-progress&#39;);<br>  });<br><br>  box1Element.addEventListener(&#39;pointerup&#39;, () =&gt; {<br>    console.info(&#39;We stop to move the box&#39;);<br>    lastCursorPositionForBoxMovement = null;<br>    lastBox1Position = null;<br>    box1Element.classList.remove(&#39;box--move-in-progress&#39;);<br>  });</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/800/1*Z1FpCcHOcLaY2sdVqjSmxw.gif" /><figcaption>We are able to move a box</figcaption></figure><p>OK let’s display our link. We should add it in our HTML:</p><pre>&lt;section class=&quot;app__body&quot;&gt;<br>  &lt;article class=&quot;box box--movable box--one&quot;&gt;&lt;/article&gt;<br>  &lt;article class=&quot;box box--second&quot;&gt;&lt;/article&gt;<br><br>  &lt;!-- Links just be declared after boxes, because the anchor positioning is based on CSS vars and they are initialized when the DOM element is injected --&gt;<br>  &lt;div class=&quot;link link--debug&quot;&gt;&lt;/div&gt;<br>&lt;/section&gt;</pre><p>And then in our CSS:</p><pre>.link {<br>  min-block-size: 2px;<br>  position: absolute;<br>  z-index: 9;<br><br>  container-type: anchored;<br>  inset-block-start: anchor(--box1 center);<br>  inset-inline-start: anchor(--box1 right);<br>  inset-inline-end: anchor(--box2 left);<br>  inset-block-end: anchor(--box2 center);<br><br>  background-image:<br>    linear-gradient(to bottom, black, black),<br>    linear-gradient(to right, black, black),<br>    linear-gradient(to bottom, black, black);<br>  background-size:<br>    2px,<br>    50% 2px,<br>    50% 2px;<br>  background-position:<br>    center,<br>    top left,<br>    bottom right;<br>  background-repeat: no-repeat;<br><br>  &amp;.link--debug {<br>    border: 1px dotted black;<br>  }<br>}</pre><p>Here what it looks like now:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/800/1*UNAELNOrfAzBhXGjtrpVLA.gif" /><figcaption>Gasp, this is not great</figcaption></figure><p>Good part: we display the link</p><p>Bad part: the link should be adapted based on the position of the movable box with the static box.</p><p>We are using the <a href="https://developer.mozilla.org/en-US/docs/Web/CSS/Guides/Anchor_positioning">CSS anchor positioning</a> api. One nice feature: the position fallback mechanism.</p><p>We could define a <a href="https://developer.mozilla.org/en-US/docs/Web/CSS/Reference/At-rules/@position-try">@position-try rule</a> to explain how the box should be displayed. We will reuse the CSS we needed to adapt on the previous article:</p><pre>@position-try --link-left-bottom {<br>  /*Moving box on left bottom of static box*/<br>  inset-block-start: anchor(--box2 center);<br>  inset-inline-start: anchor(--box1 center);<br>  inset-inline-end: anchor(--box2 center);<br>  inset-block-end: anchor(--box1 center);<br>}<br><br>@position-try --link-right-top {<br>  /*Moving box on right top of static box*/<br>  inset-block-start: anchor(--box1 center);<br>  inset-inline-start: anchor(--box2 center);<br>  inset-block-end: anchor(--box2 center);<br>  inset-inline-end: anchor(--box1 center);<br>}<br><br>@position-try --link-right-bottom {<br>  /*Moving box on right bottom of static box*/<br>  inset-block-start: anchor(--box2 center);<br>  inset-inline-start: anchor(--box2 center);<br>  inset-inline-end: anchor(--box1 center);<br>  inset-block-end: anchor(--box1 center);<br>}<br><br>@position-try --link-on-above {<br>  inset-block-start: anchor(--box1 center);<br>  inset-inline-start: anchor(--box1 center);<br>  inset-inline-end: anchor(--box2 center);<br>  inset-block-end: anchor(--box2 center);<br>}<br><br>@position-try --link-on-below {<br>  inset-block-start: anchor(--box2 center);<br>  inset-inline-start: anchor(--box1 center);<br>  inset-block-end: anchor(--box1 center);<br>  inset-inline-end: anchor(--box2 center);<br>}</pre><p><strong>Warning: this kind of rule is not CSS nested compatible and should be declared at the root of the CSS file.</strong></p><p>Now let’s use <a href="https://developer.mozilla.org/en-US/docs/Web/CSS/Reference/Properties/position-try">position-try</a> property to define the fallback logic:</p><pre>.link {<br>  /* ... */<br>  position-try:<br>    --link-on-above, --link-right-top, --link-left-bottom, --link-on-below,<br>    --link-right-bottom;<br>  /* ... */<br>}</pre><p>We have a better behavior we still need to adapt it:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/800/1*7fVldO3E4ivEAOf2H8zqAw.gif" /><figcaption>Better</figcaption></figure><p>Naivelly, we could say “Hey, let’s just adapt our @position-try rules. But if we look closer to the documentation, this kind of rule allows the following properties modification:</p><ul><li>Inset properties (logic, we are using the anchor positionning api)</li><li>Margin properties</li><li>Sizing properties</li><li>Self-alignment properties</li></ul><p>In our use case, we need to change the background of the link.</p><p>Let see how to resolve this aspect.</p><p>Maybe you noticed on the CSS link we declared:</p><pre>container-type: anchored;</pre><p>It defines t<a href="https://developer.mozilla.org/en-US/docs/Web/CSS/Reference/Properties/container-type">he type of container</a> to detect through <a href="https://developer.mozilla.org/en-US/docs/Web/CSS/Reference/At-rules/@container">@container</a>. This at-rule allows, in our situation, to be notified when a @position-try rule is applied on our container.</p><p>Great, we have an ability to adapt the background of the link depending of the position fallback status! <strong>But we should be careful: @container detects modification only on child elements of the targeted element</strong>. Meaning we have to adapt our CSS.</p><p>Let’s move the link’s display in a CSS pseudo-element:</p><pre>.link {<br>  /* ... */<br><br>  &amp;::before {<br>    content: &#39;&#39;;<br>    position: absolute;<br>    background-image:<br>      linear-gradient(to bottom, black, black),<br>      linear-gradient(to right, black, black),<br>      linear-gradient(to bottom, black, black);<br>    background-size:<br>      2px,<br>      50% 2px,<br>      50% 2px;<br>    background-position:<br>      center,<br>      top left,<br>      bottom right;<br>    background-repeat: no-repeat;<br>    top: 0;<br>    bottom: 0;<br>    left: 0;<br>    right: 0;<br>  }<br><br>  /* ... */<br>}</pre><p>Now let’s declare our @container rules (<strong>nota bene: the rules are CSS nested compliant</strong>):</p><pre>.link {<br>  /* ... */<br>  @container anchored(fallback: --link-left-bottom) {<br>    &amp;::before {<br>      background-position:<br>        0% 100%,<br>        center,<br>        100% 0%;<br>      background-size:<br>        2px 50%,<br>        100% 2px,<br>        2px 50%;<br>    }<br>  }<br><br>  @container anchored(fallback: --link-on-above) {<br>    &amp;::before {<br>      background-image:<br>        linear-gradient(#000, #000), linear-gradient(to right, #000, #000),<br>        linear-gradient(#000, #000);<br>      background-position:<br>        0 0,<br>        center,<br>        100% 100%;<br>      background-size:<br>        2px 50%,<br>        100% 2px,<br>        2px 50%;<br>    }<br>  }<br><br>  @container anchored(fallback: --link-right-top) {<br>    &amp;::before {<br>      background-image:<br>        linear-gradient(#000, #000), linear-gradient(to right, #000, #000),<br>        linear-gradient(#000, #000);<br>      background-position:<br>        0% 100%,<br>        center,<br>        100% 0;<br>      background-size:<br>        2px 50%,<br>        100% 2px,<br>        2px 50%;<br>    }<br>  }<br><br>  @container anchored(fallback: --link-on-below) {<br>    &amp;::before {<br>      background-image:<br>        linear-gradient(#000, #000), linear-gradient(to right, #000, #000),<br>        linear-gradient(#000, #000);<br>      background-position:<br>        0% 100%,<br>        center,<br>        100% 0%;<br>      background-size:<br>        2px 50%,<br>        100% 2px,<br>        2px 50%;<br>    }<br>  }<br>}</pre><p>Now, we haver the expected behaviour:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/800/1*d2sFQrCTFuws_pX8k40efQ.gif" /><figcaption>It sounds great!</figcaption></figure><p>What have we discovered in this article?</p><ol><li>We have @position-try rules capabilities but they are not CSS nested compliant</li><li>We have @container rules for anchored api, they are CSS nested compliant but it doesn’t react on the root element</li></ol><p>But with only CSS, we were able to provide a behaviour where usually SVG or full JavaScript is used to provide it.</p><p>The main caveat in my point of view: this is not (yet) generic.</p><p>The <a href="https://medium.com/@rochejul/connect-html-elements-with-only-css-part-3-use-a-bit-of-js-to-be-more-generic-ac4c8c519f87">next article </a>will use a little more JavaScript but should provide a full generic solution.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=21152c1c5998" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Connect HTML elements with only CSS — part 1 (design the link)]]></title>
            <link>https://medium.com/@rochejul/connect-html-elements-with-only-css-part-1-design-the-link-bcad92592d39?source=rss-67bfe57c1d45------2</link>
            <guid isPermaLink="false">https://medium.com/p/bcad92592d39</guid>
            <category><![CDATA[css]]></category>
            <dc:creator><![CDATA[Julien Roche]]></dc:creator>
            <pubDate>Sat, 31 Oct 2026 13:35:20 GMT</pubDate>
            <atom:updated>2026-10-31T13:35:20.737Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/840/1*63NS4kW1F5K_RwkVDt8nKw.png" /><figcaption>How could we connect HTML elements like a classical graph?</figcaption></figure><p>I recall the days where providing a complex UI, such as charts or UML diagrams.</p><p>Some solutions use SVG, such as <a href="https://www.jointjs.com/demos/force-directed-layout">JointJs</a>.</p><p>Some solutions use HTML Canvas.</p><p>Did we have another kind of solutions? When we look the current HTML/CSS state, couldn’t we do something with only CSS and very few JavaScript?</p><p>I made three articles to talk about some findings, using only CSS as much as possible and using if necessary (depending of what you want to do) some (minor) JavaScript.</p><p>This article is focused on the link’s design and you could find <a href="https://github.com/rochejul/medium-connect-elements-with-css-part1">here</a> the repository with the referenced code.</p><p>In this article, we will be focused on the usage of native CSS, without extra stuff.</p><p>Let’s take some minutes to think about it. What is our need? To “link” two boxes. A kind of anchors, right? Then, we will use the <a href="https://developer.mozilla.org/en-US/docs/Web/CSS/Guides/Anchor_positioning">CSS anchor positioning</a> api for that!</p><p>First, we need to define some basics. We will start with the boxes.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/994/1*MDEjVk_kYPiARlteprnyTg.png" /><figcaption>The boxes we want to link</figcaption></figure><p>On our HTML code, it will look like:</p><pre>&lt;article class=&quot;box box--one&quot;&gt;&lt;/article&gt;<br>&lt;article class=&quot;box box--second&quot;&gt;&lt;/article&gt;</pre><p>And with this simple CSS:</p><pre>.box {<br>  box-shadow: 10px 10px 20px 5px rgba(0, 0, 0, 0.5);<br>  border: 1px solid transparent;<br>  border-radius: 1rem;<br>  pointer-events: none;<br>  position: absolute;<br>  height: 5rem;<br>  width: 5rem;<br><br>  &amp;.box--one {<br>    anchor-name: --box1;<br>    background-color: lightgray;<br>    top: 25px;<br>    left: 55px;<br>  }<br><br>  &amp;.box--second {<br>    anchor-name: --box2;<br>    background-color: lightgreen;<br>    top: 185px;<br>    left: 505px;<br>  }<br>}</pre><p>We could see we have defined <a href="https://developer.mozilla.org/en-US/docs/Web/CSS/Reference/Properties/anchor-name">anchor-name</a> to “refer” the two boxes.</p><p>Now, let’s display our link.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/994/1*UG3x8ktt94k2A1Clcsg0gg.png" /><figcaption>The link is represented with the dotted element</figcaption></figure><p>In the CSS, we will add something to “debug” what we do for the moment (to show the behavior/result) and add the expected configuration. It will look like:</p><pre>.link {<br>  min-block-size: 2px;<br>  position: absolute;<br><br>  &amp;.link--debug {<br>    border: 1px dotted black;<br>  }<br><br>  &amp;.link--box1-to-box2 {<br>    /*Box 1 on left top of box 2*/<br>    /**/<br>    inset-block-start: anchor(--box1 center);<br>    inset-inline-start: anchor(--box1 right);<br>    inset-inline-end: anchor(--box2 left);<br>    inset-block-end: anchor(--box2 center);<br>  }<br>}</pre><p>No magic trick or woodoo ritual, we use inset properties to specify how to connect the link to the boxes. For example, we specify the top/left of the link element is connected to the right/center of the first box.</p><p>Now modify our HTML like so:</p><pre>&lt;article class=&quot;box box--one&quot;&gt;&lt;/article&gt;<br>&lt;article class=&quot;box box--second&quot;&gt;&lt;/article&gt;<br><br>&lt;!--<br>  Links just be declared after boxes,<br>  because the anchor positioning is based on CSS vars<br>  and they are initialized when the DOM element is injected<br>--&gt;<br>&lt;div class=&quot;link link--debug link--box1-to-box2&quot;&gt;&lt;/div&gt;</pre><p>As you could notice, if we add the link before the boxes, it will not work, because when the box is rendered, the CSS engine will interpret the CSS and initialize the CSS variable. As we have a sequential rendering, we have to define the link after the boxes.</p><p>At this time, our link doesn’t look like to link/line: this is a basic block. But before fixing the aspect, let’s fix the position.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/994/1*X4Ao5KV0wRaaC42hEJT5aA.png" /><figcaption>Better position</figcaption></figure><p>Rather to really try to connect box’s edges to the link element, we could do instead a connection from the middle of the box. It will simplify the reasoning and later, we will see it will improve the UX.</p><p>We need only to add some z-index and to change the anchor configuration:</p><pre>.box {<br>  /* ... */<br>  z-index: 10;<br><br>  /* ... */<br>}<br><br>.link {<br>  /* ... */<br>  z-index: 9;<br><br>  /* ... */<br><br>  &amp;.link--box1-to-box2 {<br>    inset-block-start: anchor(--box1 center);<br>    inset-inline-start: anchor(--box1 center);<br>    inset-inline-end: anchor(--box2 center);<br>    inset-block-end: anchor(--box2 center);<br>  }<br>}</pre><p>Ok, now, let’s improve the UX. We will do iteration for that.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/994/1*xtAo2yJfzpYNzFO5mCxOjw.png" /><figcaption>First naive iteration</figcaption></figure><p>For the moment, we have a box on the to/left of another box. We will use the top and right border of the link to draw our “link”.</p><pre>.link {<br>  /* ... */<br><br>  &amp;.link--box1-to-box2 {<br>    /* ... */<br><br>    border-right: 1px solid black;<br>    border-top: 1px solid black;<br>  }<br>}</pre><p>Ok, we were able to connect the boxes using only CSS. But when we look diagrams, we have usually squigglink link.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/994/1*658BUCoaHwsrO_cmR1BsYQ.png" /><figcaption>Squiggling link</figcaption></figure><p>With borders, it seems not feasible.</p><p>We use another modern CSS trick: the multiple <a href="https://developer.mozilla.org/en-US/docs/Web/CSS/Reference/Properties/background-image">background-image</a></p><p>Indeed, a background image could display an image, a gradient, and most of all, could have multiple declarations with various <a href="https://developer.mozilla.org/en-US/docs/Web/CSS/Reference/Properties/background-size">background-size</a> and <a href="https://developer.mozilla.org/en-US/docs/Web/CSS/Reference/Properties/background-position">background-position</a>.</p><p>And using <a href="https://developer.mozilla.org/en-US/docs/Web/CSS/Reference/Values/gradient/linear-gradient">linear-gradient() </a>will help to draw our lines. It will look like:</p><pre>link {<br>  background-repeat: no-repeat;<br>  /* ... */<br><br>  &amp;.link--box1-to-box2 {<br>    /* ... */<br><br>    background-image:<br>      linear-gradient(to bottom, black, black),<br>      linear-gradient(to right, black, black),<br>      linear-gradient(to bottom, black, black);<br>    background-size:<br>      2px,<br>      50% 2px,<br>      50% 2px;<br>    background-position:<br>      center,<br>      top left,<br>      bottom right;<br>  }<br>}</pre><p>Let’s add another box, on the right/middle of the second box. We will have the exact CSS and it will look like:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/994/1*KDgmgheSekA0T4fegydlCg.png" /><figcaption>Adaptive link</figcaption></figure><p>With the above CSS, we have a kind of adaptive link. Indeed, depending of the position of the boxes, we will have a squiggling link or a simple line.</p><p>But we are not in a fairy tale and we will face some issues when the third box will be below of the second box.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/994/1*4OIsm-NDZCm9nZUMcwz02w.png" /><figcaption>Adaptive link, but we have to change some CSS too</figcaption></figure><p>Indeed, we have to readjust the background images, depending of the situation.</p><p>In the above case, we simply need to do:</p><pre>.link {<br>  /* ... */<br><br>  &amp;.link--box3-to-box2 {<br>    /* ... */<br>    background-position:<br>      center,<br>      0 100%,<br>      100% 0;<br>  }<br>}</pre><p>And that’s it!</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/994/1*ln0tALqmrtf9W5_XZAMAeA.png" /><figcaption>Now it’s fine</figcaption></figure><p>At the end, we need to adapt for 4 situations.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/994/1*7rBr-bhJbQaNhy8Xx5n-wA.png" /><figcaption>The 4 situations</figcaption></figure><p>We need to draw the squiggling link with the following 4 rules:</p><pre>.link {<br>  /* ... */<br><br>  &amp;.link--box1-to-box2 {<br>    /*Box 1 on left top of box 2*/<br>    /* ... */<br><br>    background-image:<br>      linear-gradient(to bottom, black, black),<br>      linear-gradient(to right, black, black),<br>      linear-gradient(to bottom, black, black);<br>    background-size:<br>      2px,<br>      50% 2px,<br>      50% 2px;<br>    background-position:<br>      center,<br>      top left,<br>      bottom right;<br>  }<br><br>  &amp;.link--box3-to-box2 {<br>    /*Box 3 on left bottom of box 2*/<br>    /* ... */<br><br>    background-image:<br>      linear-gradient(to bottom, black, black),<br>      linear-gradient(to right, black, black),<br>      linear-gradient(to bottom, black, black);<br>    background-size:<br>      2px,<br>      50% 2px,<br>      50% 2px;<br>    background-position:<br>      center,<br>      0 100%,<br>      100% 0;<br>  }<br><br>  &amp;.link--box4-to-box2 {<br>    /*Box 4 on right top of box 2*/<br>    /* ... */<br><br>    background-image:<br>      linear-gradient(to bottom, black, black),<br>      linear-gradient(to right, black, black),<br>      linear-gradient(to bottom, black, black);<br>    background-size:<br>      2px,<br>      50% 2px,<br>      50% 2px;<br>    background-position:<br>      center,<br>      0 100%,<br>      100% 0;<br>  }<br><br>  &amp;.link--box5-to-box2 {<br>    /*Box 5 on right bottom of box 2*/<br>    /* ... */<br><br>    background-image:<br>      linear-gradient(to bottom, black, black),<br>      linear-gradient(to right, black, black),<br>      linear-gradient(to bottom, black, black);<br>    background-size:<br>      2px,<br>      50% 2px,<br>      50% 2px;<br>    background-position:<br>      center,<br>      top left,<br>      bottom right;<br>  }<br>}</pre><p>We could see it was easy to use CSS to link elements between them, link in a diagram.</p><p>In <a href="https://medium.com/@rochejul/connect-html-elements-with-only-css-part-2-automatically-position-with-only-css-21152c1c5998">the next article,</a> we will be focused how to adapt the CSS to automatically adapt the suqiggling link depending of the boxes’ positions.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=bcad92592d39" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[CSS mixin is not yet available. Any fallback?]]></title>
            <link>https://medium.com/@rochejul/css-mixin-is-not-yet-available-any-fallback-ccc85848e421?source=rss-67bfe57c1d45------2</link>
            <guid isPermaLink="false">https://medium.com/p/ccc85848e421</guid>
            <category><![CDATA[css]]></category>
            <dc:creator><![CDATA[Julien Roche]]></dc:creator>
            <pubDate>Mon, 24 Aug 2026 14:26:21 GMT</pubDate>
            <atom:updated>2026-08-24T14:26:21.179Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/700/0*ZXOWFQpkQmsHM-wh" /><figcaption>Dig in CSS mixin</figcaption></figure><p>For more than a decade, I used various CSS augmented tools, such as <a href="https://lesscss.org/">Less.js</a>, <a href="https://stylus-lang.com/">Stylus</a> and <a href="https://sass-lang.com/">Sass</a>.</p><p>But with the raise of CSS 5 and CSS 6, we have more and more features natively integrated in our browser, such as <a href="https://developer.mozilla.org/en-US/docs/Web/CSS/Guides/Nesting/Using">CSS nesting</a>, <a href="https://developer.mozilla.org/en-US/docs/Web/CSS/Reference/Values/if">CSS if() function</a>, etc…</p><p>However, at this time, there is still a missing piece. We have the ability to declare <a href="https://developer.mozilla.org/en-US/docs/Web/CSS/Guides/Custom_functions_and_mixins">a custom function to generate an appropriate value for one property</a>:</p><pre>@function --transparent(--color &lt;color&gt;, --alpha &lt;number&gt;: 0.8) returns &lt;color&gt; {<br>  result: oklch(from var(--color) l c h / var(--alpha));<br>}<br><br>section {<br>  --base-color: #faa6ff;<br>  background-color: --transparent(var(--base-color));<br>}</pre><p>However the at-rules @mixin and @apply are still not available (despite they are in the <a href="https://drafts.csswg.org/css-mixins/">W3C draft</a> and the <a href="https://github.com/w3c/csswg-drafts/issues/9350#issuecomment-1939628591">CSS working group started to look on it</a>).</p><p>With this feature, we should be able to inject a bunch of properties (and nested rules) in a CSS selector with or without arguments. Example (<a href="https://css-tricks.com/css-functions-and-mixins-module-notes/">from CSS tricks</a>):</p><pre>@mixin --gradient-linear(<br>  --color-1 type(color),<br>  --color-2 type(color),<br>  --angle type(angle),<br>) {<br>  --from: var(--color-1, orangered);<br>  --to: var(--from-color, goldenrod);<br>  --angle: var(--at-angle, to bottom right);<br><br>  background: linear-gradient(var(--angle), var(--from), var(--to));<br><br>  @media (prefers-contrast: more) {<br>    background: color-mix(var(--from), black);<br>    color: white;<br>  }<br>}<br><br>.some-class {<br>  @apply --gradient-linear(red, blue, 80deg);<br>}</pre><p>Does it mean we still need a CSS tooling framework? Not necessarily</p><p>Indeed, by using the scope of <a href="https://developer.mozilla.org/en-US/docs/Web/CSS/Guides/Cascading_variables/Using_custom_properties">CSS vars</a>, we could reproduce the behavior.</p><p>For one of my POC repository, I wanted to declare <a href="https://rochejul.github.io/experimental-dashboards/">connectors between elements, using only CSS</a>:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/686/1*Gg7AnjMGPQ7vHXGR-W5ZYg.png" /><figcaption>One connector only</figcaption></figure><p>The problem occurs when we have multiple elements:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/867/1*jGiGW6kTaCHm0kJ_mPWvzw.png" /><figcaption>Multiple connectors</figcaption></figure><p>Indeed, the POC uses the <a href="https://developer.mozilla.org/en-US/docs/Web/CSS/Guides/Anchor_positioning">CSS anchor positioning API</a> where the boxes use the property <a href="https://developer.mozilla.org/en-US/docs/Web/CSS/Reference/Properties/anchor-name">anchor-name</a> to be identified but and so the connectors are based on the anchor-names.</p><p>However, in a naive approach, it means we need to duplicate the CSS for each connectors. Which is not great.</p><p>However we could simplify it by using inline CSS vars.</p><p>Indeed, let’s declare the following CSS:</p><pre>.link {<br>  /* ... */<br><br>  &amp;.link--left-top {<br>    /*Moving box on left top of static box*/<br>    inset-block-start: anchor(var(--moving-element) center);<br>    inset-inline-start: anchor(var(--moving-element) right);<br>    inset-inline-end: anchor(var(--static-element) left);<br>    inset-block-end: anchor(var(--static-element) center);<br>  }<br><br>  /* ... */<br>}</pre><p>On the above CSS, the block contains two CSS vars: <em>— moving-element</em> and <em>— static-element</em>. You could notice the variables are not initialized. But we should, are not initialized <em>yet</em>.</p><p>Indeed, let’s have a look on the HTML side:</p><pre>&lt;div<br>  style=&quot;--moving-element: --box1; --static-element: --box2&quot;<br>  class=&quot;link link--left-top&quot;<br>&gt;&lt;/div&gt;</pre><p>We could see we define the two CSS vars in the <em>style</em> attribute. We inject the anchor-names we have declared on the two boxes. Even we declare another connector for the same CSS class but for two differents boxes, it will work, because the variables scoping is limited to the connector itself (the <em>&lt;div&gt;</em> element).</p><p>With this approach, we are able to define a kind of mixins without using CSS tooling.</p><p>I hope it could help.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ccc85848e421" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Carousel CSS APi — a short overview]]></title>
            <link>https://medium.com/@rochejul/carousel-css-api-a-short-overview-1eb329e25e66?source=rss-67bfe57c1d45------2</link>
            <guid isPermaLink="false">https://medium.com/p/1eb329e25e66</guid>
            <category><![CDATA[css]]></category>
            <category><![CDATA[carousel]]></category>
            <dc:creator><![CDATA[Julien Roche]]></dc:creator>
            <pubDate>Wed, 10 Jun 2026 09:51:21 GMT</pubDate>
            <atom:updated>2026-06-10T09:51:21.280Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/700/0*SMvkib7mWmYGf6Wu" /><figcaption>Let’s discover the CSS Carousel API</figcaption></figure><p>I made <a href="https://github.com/rochejul/medium-carousel">this tiny repository</a> (and a <a href="https://rochejul.github.io/medium-carousel/">live demo</a>) to share a concrete example of the CSS API as <a href="https://mobbin.com/glossary/carousel">a carousel</a>.</p><p>Little disclaimer: the API works at this time only for <a href="https://caniuse.com/wf-scroll-markers">Blink based Browser.</a> Meaning Firefox or Safari doesn’t support it at this time.</p><p>First, let’s implement <a href="https://github.com/rochejul/medium-carousel/commit/fd7724e5cf7f389eb58c1a0ba578d93e968f3cf2">a basic carousel</a>:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/880/1*d15A5K-LU3QFq5Cys31epQ.gif" /><figcaption>Basic</figcaption></figure><p>Our HTML template looks like:</p><pre>&lt;section class=&quot;carousel&quot;&gt;<br>    &lt;article class=&quot;carousel__item&quot;&gt;&lt;/article&gt;<br>    &lt;article class=&quot;carousel__item&quot;&gt;&lt;/article&gt;<br>    &lt;!-- ... --&gt;<br>  &lt;/section&gt;</pre><p>We use the <a href="https://developer.mozilla.org/en-US/docs/Web/CSS/Reference/Properties/display#grid">display mode grid</a> with the <a href="https://developer.mozilla.org/en-US/docs/Web/CSS/Reference/Properties/grid-auto-flow#column">grid-auto-flow to column</a> to display the various images.</p><p>We provide also the following CSS to pilot the scrolling experience:</p><pre>.carousel {<br>  overflow-x: auto;<br>  overscroll-behavior-x: contain;<br>  scrollbar-gutter: stable;<br>}</pre><p>The use here of <a href="https://developer.mozilla.org/en-US/docs/Web/CSS/Reference/Properties/scrollbar-gutter#stable">scrollbar-gutter to stable</a> means even if we needn’t scroll in the carousel (not enough images), the space to display the scrollbar is “present”. Meaning if we dynamically add images (or reduce the browser’s width), there will no “jump” because we display the scrollbar.</p><p><a href="https://developer.mozilla.org/en-US/docs/Web/CSS/Reference/Properties/overscroll-behavior#contain">overscroll-behavior-x</a> will be used to avoid some additional scrolling behaviour like the “pull-to-refresh”</p><p><a href="https://github.com/rochejul/medium-carousel/commit/dba1f50c6cfa8a959800dc8d68032a41b31f87e7">Let’s evolve our carousel to configure the scrolling behaviour</a>. We will inject the following code:</p><pre>.carousel {<br>  /* ... */<br><br>  scroll-behavior: smooth;<br>  scroll-snap-type: x mandatory;<br>  scroll-snap-align: center;<br><br>  &amp; &gt; .carousel__item {<br>    scroll-snap-align: center;<br>  }<br>}</pre><p><a href="https://developer.mozilla.org/en-US/docs/Web/CSS/Reference/Properties/scroll-behavior#smooth">scroll-behavior</a> is configured to provide a smooth transition when we scroll. Also, we use the <a href="https://developer.mozilla.org/en-US/docs/Glossary/Scroll_snap">Scroll Snap API</a> to coordinate the scroll with the content, so in our case with the images of the carousel:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/880/1*s-sV1nNNCaiHgAxQSpkEDQ.gif" /><figcaption>Smooth scrolling with snapping effect</figcaption></figure><p>Now, let’s go to the first major step: <a href="https://github.com/rochejul/medium-carousel/commit/818cb97f807102471e4e00306b7e81589fd1c626">to display buttons</a> and <a href="https://github.com/rochejul/medium-carousel/commit/eb7e68f2cc9a379a9466624134588cf760882017">to hide the scrollbar</a>. If we add the following CSS:</p><pre>.carousel {<br>  /* ... */<br><br>  /* Carousel - global config */<br>  &amp;.carousel-hide-scrollbar {<br>    scrollbar-width: none;<br>  }<br><br>  /* Carousel - button mode */<br>  &amp;.carousel--button,<br>  &amp;:not(.carousel--button) {<br>    &amp;::scroll-button(left) {<br>      content: &#39;⬅&#39; / &#39;Scroll Left&#39;;<br>      left: calc(anchor(left) + 1rem);<br>      position: absolute;<br>      position-anchor: auto;<br>      top: calc(anchor(bottom) + 0.75rem);<br>    }<br><br>    &amp;::scroll-button(right) {<br>      content: &#39;⮕&#39; / &#39;Scroll Right&#39;;<br>      right: calc(anchor(right) + 1rem);<br>      position: absolute;<br>      position-anchor: auto;<br>      top: calc(anchor(bottom) + 0.75rem);<br>    }<br><br>    &amp;::scroll-button(*) {<br>      border-radius: 1rem;<br><br>      &amp;:focus-visible {<br>        outline-offset: 0.5rem;<br>      }<br>    }<br>  }<br>}</pre><p>We have then:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/880/1*mvDNDt6okz4PPQeeLElPKA.gif" /><figcaption>We have buttons to scroll within the carousels</figcaption></figure><p>We use the new pseudo-element <a href="https://developer.mozilla.org/en-US/docs/Web/CSS/Reference/Selectors/::scroll-button">::scroll-button()</a>, allowing to 1) display buttons and 2) customize then (using <strong>right</strong>, <strong>left</strong> or <strong>*</strong>). We could also use <strong>up</strong> and <strong>down</strong> (and <a href="https://drafts.csswg.org/css-box-4/">CSS Box model</a> values).</p><p>We have the possibilities to apply on the scroll buttons various pseudo-classes like:</p><ul><li><a href="https://developer.mozilla.org/en-US/docs/Web/CSS/Reference/Selectors/:focus-visible">:focus-visible</a></li><li><a href="https://developer.mozilla.org/en-US/docs/Web/CSS/Reference/Selectors/:focus">:focus</a></li><li><a href="https://developer.mozilla.org/en-US/docs/Web/CSS/Reference/Selectors/:disabled">:disabled</a></li><li><a href="https://developer.mozilla.org/en-US/docs/Web/CSS/Reference/Selectors/:active">:active</a></li><li><a href="https://developer.mozilla.org/en-US/docs/Web/CSS/Reference/Selectors/:hover">:hover</a></li><li>etc…</li></ul><p>We could notice automatically, the buttons state are updated depending of the scroll (with the buttons or with the scrollbar). And here what they look like in the DOM:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/487/1*_EofJltpbAwYeSdM0aol_w.png" /><figcaption>DOM inspection for our buttons</figcaption></figure><p>The CSS Carousel API has got a notion of markers. Let’s modify our carousel <a href="https://github.com/rochejul/medium-carousel/commit/faf8797ba4ca6c9e25347d15a641c9bb592aab75">to add the markers</a>:</p><pre>.carousel {<br>  /* ... */<br><br>  /* Carousel - marker mode */<br>  &amp;.carousel--marker {<br>    scroll-marker-group: after;<br><br>    &amp;::scroll-marker-group {<br>      display: flex;<br>      height: 1.5rem;<br>      content: counter(markers);<br>      gap: 0.5rem;<br>      justify-content: center;<br>      left: calc(anchor(left) + 4rem);<br>      overflow: auto; /* Allow scrolling if too many scroll markers are inserted. */<br>      position: absolute;<br>      position-anchor: auto;<br>      right: calc(anchor(right) + 4rem);<br>      top: calc(anchor(bottom) + 1rem);<br>    }<br><br>    &amp; &gt; .carousel__item:has(::scroll-marker:target-current) {<br>      border-color: red;<br>    }<br><br>    &amp; &gt; .carousel__item::scroll-marker {<br>      background: lightgrey;<br>      border: 1px solid black;<br>      border-radius: 1rem;<br>      content: &#39;&#39;;<br>      height: 1rem;<br>      width: 1rem;<br>    }<br><br>    &amp; &gt; .carousel__item::scroll-marker:target-current {<br>      /* For some reason, it seems it doesn&#39;t like the nested rules */<br>      background: red;<br>    }<br>  }<br><br>  /* Carousel - button mode */<br>  &amp;.carousel--button,<br>  &amp;:not(.carousel--button, .carousel--marker) {<br>    /* ... */<br>  }<br>}</pre><p>Which looks like:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/880/1*bwZNKaBzNpiyFMCmWvTPzw.gif" /><figcaption>We have markers, and we could mixin markers ith buttons</figcaption></figure><p>For each carousel’s image, we display a marker to scroll to the image. We could see <strong>we could have both markers and buttons and they are synchronized</strong>.</p><p>On the carousel itself, we declared a <a href="https://developer.mozilla.org/en-US/docs/Web/CSS/Reference/Properties/scroll-marker-group">scroll-marker-group</a> to inform we want to have a group of markers (here we decided to display it after the carousel element).</p><p>Then for each carousel’s image, we associate a pseudo-element <a href="https://developer.mozilla.org/en-US/docs/Web/CSS/Reference/Selectors/::scroll-marker">::scroll-marker</a>. This element will be used to scroll on the associated image when the user clicks on it. We also have the pseudo-class <a href="https://developer.mozilla.org/en-US/docs/Web/CSS/Reference/Selectors/:target-current">:target-current</a> to style the active marker (but we could use classic pseudo-element like <a href="https://developer.mozilla.org/en-US/docs/Web/CSS/Reference/Selectors/:hover">:hover</a>).</p><p>Here a DOM inspection:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/793/1*_I3baCeEYMSBd3zDEqLuNw.png" /><figcaption>DOM inspection for our markers</figcaption></figure><p>With the above CSS, if we have too many markers, we could scroll within the marker group element. But we could refine to provide a way <a href="https://github.com/rochejul/medium-carousel/commit/a50c847f71a98f84325bf6e5ead73abc9a368fc4">to display only some markers</a>:</p><pre>.carousel {<br>  /* ... */<br><br>  /* Carousel - marker mode */<br>  &amp;.carousel--marker {<br>    /* ... */<br><br>    &amp;.carousel--marker--multiple {<br>      &amp; &gt; .carousel__item::scroll-marker {<br>        display: none;<br>      }<br><br>      &amp; &gt; .carousel__item:nth-last-of-type(4n)::scroll-marker {<br>        display: block;<br>      }<br>    }<br><br>    /* ... */<br>  }<br><br>  /* ... */<br>}</pre><p>Then, we reduce the number of markers:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/880/1*1zicUS0_Wynxret0lJ7-MQ.gif" /><figcaption>“Multiple marker mode”</figcaption></figure><p>We saw in this article a nice future feature in our browser to ease the carousel’s creation within our apps.</p><p>Useful resources to dig more:</p><ul><li>Chrome Dev blog: <a href="https://developer.chrome.com/blog/carousels-with-css">https://developer.chrome.com/blog/carousels-with-css</a></li><li>Chrome Carousel configurator: <a href="https://chrome.dev/carousel-configurator/">https://chrome.dev/carousel-configurator/</a></li><li>Carousel explainer: <a href="https://flackr.github.io/carousel/">https://flackr.github.io/carousel/</a></li><li>MDN doc: <a href="https://developer.mozilla.org/en-US/docs/Web/CSS/Guides/Overflow/Carousels">https://developer.mozilla.org/en-US/docs/Web/CSS/Guides/Overflow/Carousels</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=1eb329e25e66" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Use the Popover Api to create a menu for your application]]></title>
            <link>https://medium.com/@rochejul/use-the-popover-api-to-create-a-menu-for-your-application-126576a8fc4c?source=rss-67bfe57c1d45------2</link>
            <guid isPermaLink="false">https://medium.com/p/126576a8fc4c</guid>
            <category><![CDATA[html]]></category>
            <category><![CDATA[popover]]></category>
            <dc:creator><![CDATA[Julien Roche]]></dc:creator>
            <pubDate>Wed, 27 May 2026 13:27:19 GMT</pubDate>
            <atom:updated>2026-05-27T13:27:19.064Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/700/0*MZFCtADahcZBYrz_" /><figcaption>Let’s discover the Popover Api (again)</figcaption></figure><p>I made <a href="https://github.com/rochejul/medium-popover-as-app-menu">this tiny repository</a> (and a <a href="https://rochejul.github.io/medium-popover-as-app-menu/">live demo</a>) to share a concrete example of the <a href="https://developer.mozilla.org/en-US/docs/Web/API/Popover_API">Popover Api</a> as a button with a menu.</p><p>This article is a followup of <a href="https://medium.com/@rochejul/use-the-popover-api-to-create-a-fancy-button-with-a-menu-ba926690c414">Use the Popover Api to create a fancy button with a menu</a>. The idea here: see how could we implement an app menu with the Popover Api.</p><p>Let’s start with a basic menu implementation by reusing what we saw in the previous article.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*fv3mKNtVvuHZBtUfYAGBmg.gif" /><figcaption>Basic implementation</figcaption></figure><p>For the moment, no magic in the air: we display a basic menu. The power of the api will come when we will start multiple levels.</p><p>Let’s revisit our menu and add one entry with a second level:</p><pre>&lt;div class=&quot;menu__subcontainer&quot;&gt;<br>    &lt;a href=&quot;#&quot; tabindex=&quot;0&quot;&gt;Pizza&lt;/a&gt;<br>    &lt;div popover=&quot;auto&quot;&gt;<br>      &lt;div class=&quot;menu__listcontainer&quot;&gt;<br>        &lt;a href=&quot;#&quot;&gt;Margherita&lt;/a&gt;<br>        &lt;a href=&quot;#&quot;&gt;Pepperoni&lt;/a&gt;<br>        &lt;a href=&quot;#&quot;&gt;Ham &amp;amp; Shroom&lt;/a&gt;<br>        &lt;a href=&quot;#&quot;&gt;Vegan&lt;/a&gt;<br>      &lt;/div&gt;<br>    &lt;/div&gt;<br>  &lt;/div&gt;<br>&lt;/nav&gt;</pre><pre>.menu__subcontainer {<br>  anchor-name: --submenu;<br>  display: flex;<br>  flex-direction: column;<br><br>  &amp; &gt; a {<br>    position: relative;<br><br>    &amp;::after {<br>      content: &#39;➤&#39;;<br>      font-weight: bold;<br>      position: absolute;<br>      top: 0;<br>      right: 0.5rem;<br>    }<br>  }<br><br>  &amp; [popover] {<br>    border: 1px solid black;<br>    width: 120px;<br>    inset: unset;<br><br>    top: anchor(self-start); /* === anchor(top) */<br>    left: calc(anchor(self-end) - 1rem); /* === anchor(right) */<br>    position-anchor: --submenu;<br>  }<br>}</pre><pre>const mainpopover = document.querySelector(&#39;.menu__mainpopover&#39;);<br>const subcontainers = Array.from(<br>  document.querySelectorAll(&#39;.menu__subcontainer&#39;),<br>);<br><br>subcontainers.forEach((subcontainer) =&gt; {<br>  const subpopover = subcontainer.querySelector(&#39;[popover]&#39;);<br><br>  // Event to show the subpopover when the mouse moves over<br>  subcontainer.addEventListener(&#39;pointerover&#39;, (event) =&gt; {<br>    event.stopPropagation();<br>    subpopover.showPopover();<br>  });<br><br>  // Event to hide the subpopover when the mouse moves out<br>  subcontainer.addEventListener(&#39;pointerleave&#39;, (event) =&gt; {<br>    subpopover.hidePopover();<br>  });<br>});<br><br>// Events to hide the popover menus when an option is selected in either of them<br>mainpopover.addEventListener(&#39;pointerdown&#39;, () =&gt; {<br>  subcontainers.forEach((subcontainer) =&gt; {<br>    const subpopover = subcontainer.querySelector(&#39;[popover]&#39;);<br>    subpopover.hidePopover();<br>  });<br><br>  mainpopover.hidePopover();<br>});</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*EC4eziE_rfKkwr4FSU7DYg.gif" /><figcaption>Our menu with one second level</figcaption></figure><p>First question: Why do we need JavaScript here? Simply because the attribute <a href="https://developer.mozilla.org/en-US/docs/Web/HTML/Reference/Elements/button#popovertarget">popovertarget</a> is only available for a button (where we have an explicit user interaction).</p><p>We need then to detect:</p><ul><li>When we click on a menu to close any popover used a n-level menu</li></ul><pre>mainpopover.addEventListener(&#39;pointerdown&#39;, () =&gt; {<br>  subcontainers.forEach((subcontainer) =&gt; {<br>    const subpopover = subcontainer.querySelector(&#39;[popover]&#39;);<br>    subpopover.hidePopover();<br>  });<br><br>  mainpopover.hidePopover();<br>});</pre><ul><li>When we hovered a menu with a sub-level, we should show the associated popover</li></ul><pre>subcontainer.addEventListener(&#39;pointerover&#39;, (event) =&gt; {<br>  event.stopPropagation();<br>  subpopover.showPopover();<br>});</pre><ul><li>When we leave a sub-level, we should hide the associated popover</li></ul><pre>subcontainer.addEventListener(&#39;pointerleave&#39;, (event) =&gt; {<br>    subpopover.hidePopover();<br>  });</pre><p>With very few JavaScript using the P<a href="https://developer.mozilla.org/en-US/docs/Web/API/Pointer_events">ointer api</a>, we could pilot the behaviour easily.</p><p>Let’s verify by adding a second entry with a second level-menu if it still works:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ll7RhfURTIhN_CLZ99dUFw.gif" /><figcaption>Damned, with a second entry with a second level, we have some weird behaviour</figcaption></figure><p>This is unexpected. We always open the last declared sub-menu. But the CSS is clear: we associate for each sub-menu the <a href="https://developer.mozilla.org/en-US/docs/Web/CSS/Reference/Properties/anchor-name">anchor-name</a> “ — submenu”. But for the <a href="https://developer.mozilla.org/en-US/docs/Web/CSS/Guides/Anchor_positioning/Using">anchor positioning API</a>, we declare multiple element with the same anchor-name. Meaning the engine will open the last declared HTML element using the CSS class “.menu__subcontainer”.</p><p>To resolve, we should apply a CSS only when the n-level menu is active and inject the new CSS class correclty:</p><pre> .menu__subcontainer {<br>    anchor-name: --submenu;<br>    display: flex;<br>    flex-direction: column;<br><br>    &amp;.menu__subcontainer--active {<br>      anchor-name: --submenu;<br>    }</pre><pre>subcontainers.forEach((subcontainer) =&gt; {<br>  const subpopover = subcontainer.querySelector(&#39;[popover]&#39;);<br><br>  // Event to show the subpopover when the mouse moves over<br>  subcontainer.addEventListener(&#39;pointerover&#39;, (event) =&gt; {<br>    subcontainer.classList.add(&#39;menu__subcontainer--active&#39;);<br>    event.stopPropagation();<br>    subpopover.showPopover();<br>  });<br><br>  // Event to hide the subpopover when the mouse moves out<br>  subcontainer.addEventListener(&#39;pointerleave&#39;, (event) =&gt; {<br>    subcontainer.classList.add(&#39;menu__subcontainer--active&#39;);<br>    subpopover.hidePopover();<br>  });<br>});<br><br>// Events to hide the popover menus when an option is selected in either of them<br>mainpopover.addEventListener(&#39;pointerdown&#39;, () =&gt; {<br>  subcontainers.forEach((subcontainer) =&gt; {<br>    const subpopover = subcontainer.querySelector(&#39;[popover]&#39;);<br>    subcontainer.classList.add(&#39;menu__subcontainer--active&#39;);<br>    subpopover.hidePopover();<br>  });<br><br>  mainpopover.hidePopover();<br>});</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*EJOjsUTTBSlRfjBD2BXCqw.gif" /><figcaption>Great, we have a good UX for both second level menu.</figcaption></figure><p>Great, we have resolved the issue. Now, let’s see how it will work when we add a third-level menu:</p><pre>&lt;div class=&quot;menu&quot;&gt;<br>  &lt;button popovertarget=&quot;mainpopover&quot;&gt;Menu&lt;/button&gt;<br>  &lt;div id=&quot;mainpopover&quot; popover=&quot;auto&quot; class=&quot;menu__mainpopover&quot;&gt;<br>    &lt;nav class=&quot;menu__listcontainer&quot;&gt;<br>      &lt;div class=&quot;menu__subcontainer&quot;&gt;<br>        &lt;a href=&quot;#&quot; tabindex=&quot;0&quot;&gt;Pizza&lt;/a&gt;<br>        &lt;div popover=&quot;auto&quot;&gt;<br>          &lt;div class=&quot;menu__listcontainer&quot;&gt;<br>            &lt;a href=&quot;#&quot;&gt;Margherita&lt;/a&gt;<br>            &lt;a href=&quot;#&quot;&gt;Pepperoni&lt;/a&gt;<br>            &lt;a href=&quot;#&quot;&gt;Ham &amp;amp; Shroom&lt;/a&gt;<br>            &lt;a href=&quot;#&quot;&gt;Vegan&lt;/a&gt;<br>            &lt;div class=&quot;menu__subcontainer&quot;&gt;<br>              &lt;a href=&quot;#&quot; tabindex=&quot;0&quot;&gt;Weird&lt;/a&gt;<br>              &lt;div popover=&quot;auto&quot;&gt;<br>                &lt;div class=&quot;menu__listcontainer&quot;&gt;<br>                  &lt;a href=&quot;#&quot;&gt;Hawai&lt;/a&gt;<br>                  &lt;a href=&quot;#&quot;&gt;Nutella&lt;/a&gt;<br>                &lt;/div&gt;<br>              &lt;/div&gt;<br>            &lt;/div&gt;<br>          &lt;/div&gt;<br>        &lt;/div&gt;<br>      &lt;/div&gt;<br>      &lt;a href=&quot;#&quot;&gt;Music&lt;/a&gt;<br>      &lt;a href=&quot;#&quot;&gt;Wombats&lt;/a&gt;<br>      &lt;a href=&quot;#&quot;&gt;Finland&lt;/a&gt;<br>      &lt;div class=&quot;menu__subcontainer&quot;&gt;<br>        &lt;a href=&quot;#&quot; tabindex=&quot;0&quot;&gt;Pizza&lt;/a&gt;<br>        &lt;div popover=&quot;auto&quot;&gt;<br>          &lt;div class=&quot;menu__listcontainer&quot;&gt;<br>            &lt;a href=&quot;#&quot;&gt;Margherita&lt;/a&gt;<br>            &lt;a href=&quot;#&quot;&gt;Pepperoni&lt;/a&gt;<br>            &lt;a href=&quot;#&quot;&gt;Ham &amp;amp; Shroom&lt;/a&gt;<br>            &lt;a href=&quot;#&quot;&gt;Vegan&lt;/a&gt;<br>          &lt;/div&gt;<br>        &lt;/div&gt;<br>      &lt;/div&gt;<br>    &lt;/nav&gt;<br>  &lt;/div&gt;<br>&lt;/div&gt;</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*wdCobg1x9t4k2ttb1U1DdQ.gif" /><figcaption>We have now n-level if menus!</figcaption></figure><p>It works… out of the box! We don’t need to add more JavaScript or CSS to deal with the Popover api multiple menu levels: what we have produced is only what we need.</p><p>We should however revisit a little our JavaScript to also manage the keyboard navigation:</p><pre>const mainpopover = document.querySelector(&#39;.menu__mainpopover&#39;);<br>const subcontainers = Array.from(<br>  document.querySelectorAll(&#39;.menu__subcontainer&#39;),<br>);<br><br>subcontainers.forEach((subcontainer) =&gt; {<br>  const subpopover = subcontainer.querySelector(&#39;[popover]&#39;);<br><br>  [<br>    &#39;pointerover&#39;, // Event to show the subpopover when the mouse moves over<br>    &#39;focusin&#39;, // Event to make the subpopover keyboard-accessible<br>  ].forEach((eventName) =&gt; {<br>    subcontainer.addEventListener(eventName, (event) =&gt; {<br>      subcontainer.classList.add(&#39;menu__subcontainer--active&#39;);<br>      event.stopPropagation();<br>      subpopover.showPopover();<br>    });<br>  });<br><br>  // Event to hide the subpopover when the mouse moves out<br>  subcontainer.addEventListener(&#39;pointerleave&#39;, (event) =&gt; {<br>    subcontainer.classList.remove(&#39;menu__subcontainer--active&#39;);<br>    subpopover.hidePopover();<br>  });<br><br>  // Event to hide the subpopover with keyboard-accessible<br>  subcontainer.addEventListener(&#39;focusout&#39;, (event) =&gt; {<br>    if (!subcontainer.contains(event.relatedTarget)) {<br>      // event.relatedTarget contains the active element<br>      subcontainer.classList.remove(&#39;menu__subcontainer--active&#39;);<br>      subpopover.hidePopover();<br>    }<br>  });<br>});<br><br>// Events to hide the popover menus when an option is selected in either of them<br>mainpopover.addEventListener(&#39;pointerdown&#39;, () =&gt; {<br>  subcontainers.forEach((subcontainer) =&gt; {<br>    const subpopover = subcontainer.querySelector(&#39;[popover]&#39;);<br>    subcontainer.classList.remove(&#39;menu__subcontainer--active&#39;);<br>    subpopover.hidePopover();<br>  });<br><br>  mainpopover.hidePopover();<br>});</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*FJfn5Oe9YVU8XPqV4BIiCg.gif" /><figcaption>Keyboard navigation enabled</figcaption></figure><p>The main change for keyboard navigation is:</p><pre>subcontainer.addEventListener(&#39;focusout&#39;, (event) =&gt; {<br>  if (!subcontainer.contains(event.relatedTarget)) {<br>    // event.relatedTarget contains the active element<br>    subcontainer.classList.remove(&#39;menu__subcontainer--active&#39;);<br>    subpopover.hidePopover();<br>  }<br>});</pre><p>In the above snippet:</p><ul><li>We react when subcontainer detects in its children three a <a href="https://developer.mozilla.org/en-US/docs/Web/API/Element/focusout_event">focusout</a> event</li><li>We inspect the <a href="https://developer.mozilla.org/en-US/docs/Web/API/FocusEvent/relatedTarget">relatedTarget</a> property which is the element having the focus. If the element is a direct child of the current subcontainer, then we close the sub-levels</li></ul><p>That’s it! With very few code, we were able to provide an application menu.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=126576a8fc4c" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Use the Popover Api to create a fancy button with a menu]]></title>
            <link>https://medium.com/@rochejul/use-the-popover-api-to-create-a-fancy-button-with-a-menu-ba926690c414?source=rss-67bfe57c1d45------2</link>
            <guid isPermaLink="false">https://medium.com/p/ba926690c414</guid>
            <category><![CDATA[css]]></category>
            <category><![CDATA[html]]></category>
            <category><![CDATA[html5]]></category>
            <category><![CDATA[popover]]></category>
            <dc:creator><![CDATA[Julien Roche]]></dc:creator>
            <pubDate>Wed, 13 May 2026 13:07:41 GMT</pubDate>
            <atom:updated>2026-05-13T13:07:41.319Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/700/0*hcRTxI9XRYQCIpqi" /><figcaption>Let’s discover the Popover Api</figcaption></figure><p>I made <a href="https://github.com/rochejul/medium-popover-as-button-with-menu">this tiny repository</a> (and a <a href="https://rochejul.github.io/medium-popover-as-button-with-menu/">live demo</a>) to share a concrete example of the <a href="https://developer.mozilla.org/en-US/docs/Web/API/Popover_API">Popover Api</a> as a button with a menu.</p><p>The <a href="https://developer.mozilla.org/en-US/docs/Web/API/Popover_API">Popover Api</a> provides a collection of HTML attributes/DOM event and JavaScript methods to pilot a popover. Plus the API is DOM driven, meaning we could pilot most of the flows only with HTML.</p><p>A very common pattern on the web is to show content over the top of other content, drawing the user’s attention to specific important information or actions that need to be taken. We have two mode then:</p><ul><li>Non-modal: The rest of the page can be interacted with while the popover is being shown. Usually, we pilot to automatically close the popover.</li><li>Modal: While a popover is being shown, the rest of the page is rendered non-interactive until the popover is actioned in some way.</li></ul><p>Let’s discover how the browser interprets a popover by using the MDN basic example:</p><pre>&lt;button popovertarget=&quot;mypopover&quot;&gt;Toggle the popover&lt;/button&gt;<br>&lt;div id=&quot;mypopover&quot; popover&gt;Popover content&lt;/div&gt;</pre><p>We could see a correlation between the <a href="https://developer.mozilla.org/en-US/docs/Web/HTML/Reference/Elements/button#popovertarget">popovertarget</a> attribute on the button and the <strong>id</strong> attribute of the div. The attribute <a href="https://developer.mozilla.org/en-US/docs/Web/HTML/Reference/Global_attributes/popover">popover</a> informs how it should react (by <a href="https://developer.mozilla.org/en-US/docs/Web/HTML/Reference/Global_attributes/popover">default</a>, the value is <strong>default</strong> meaning everything could be driven with HTML. If you want some explicit behavior with JavaScript, you could provide a <strong>manual</strong> value).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*EytlSXuxKxymD0s5I3VF-A.gif" /><figcaption>Very basic UX</figcaption></figure><p>On the above recording, we could see the behavior:</p><ul><li>Click on the button and you could open/close the popover.</li><li>Click outside of the popover and it will be automatically closed.</li><li>Click within the popover and the popover is not closed.</li></ul><p>Let’s inspect the CSS when the popover is closed:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/433/1*MDJuZfGq-RupYJG-dXvioQ.png" /><figcaption>Styling when the popover is closed</figcaption></figure><p>We have some information how the popover will behave: we could see the property <a href="https://developer.mozilla.org/en-US/docs/Web/CSS/Reference/Properties/position-anchor">position-anchor</a> meaning by nature, the popover will be related to the element which declared the <strong>popovertarget</strong> attribute.</p><p>We could also see the property <a href="https://developer.mozilla.org/en-US/docs/Web/CSS/Reference/Properties/overlay">overlay</a> to specify the way the popover should be displayed in the top layer. And the pseudo-element <a href="https://developer.mozilla.org/en-US/docs/Web/CSS/Reference/Selectors/:popover-open">:popover-open</a> to inform when the popover is displayed.</p><p>Now let’s inspect when the popover is opened:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/433/1*5QqzFR10fnb-mputbYeHUw.png" /><figcaption>Styling when the popover is opened</figcaption></figure><p>First, we see we could have the pseudo-element <a href="https://developer.mozilla.org/en-US/docs/Web/CSS/Reference/Selectors/::backdrop">::backdrop</a> similar to the <strong>dialog</strong> element (see <a href="https://medium.com/@rochejul/use-the-dialog-element-as-a-drawer-in-your-application-d960e90a6546">this article</a> for more details).</p><p>And we could see when the popover is opened, only the <strong>display</strong> and <strong>overlay</strong> attributes have changed.</p><p>Let’s change some styling and content to represent a button with a menu:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*LzNq-64NsHGdNkmhGHBC1g.gif" /><figcaption>Some basic CSS to give a better UX</figcaption></figure><p>For the moment, let’s be focused on the HTML part:</p><pre>&lt;button name=&quot;menu&quot; popovertarget=&quot;mypopover&quot;&gt;Menu A&lt;/button&gt;<br>&lt;div id=&quot;mypopover&quot; popover=&quot;auto&quot;&gt;<br>  &lt;div&gt;<br>    &lt;button popovertarget=&quot;mypopover&quot; popovertargetaction=&quot;hide&quot;&gt;Option A&lt;/button&gt;<br>  &lt;/div&gt;<br>  &lt;div&gt;<br>    &lt;button popovertarget=&quot;mypopover&quot; popovertargetaction=&quot;hide&quot;&gt;Option B&lt;/button&gt;<br>  &lt;/div&gt;<br>&lt;/div&gt;</pre><p>We could see two in the buttons with the popover we also used the <strong>popovertarget</strong> attribute plus the <a href="https://developer.mozilla.org/en-US/docs/Web/API/HTMLButtonElement/popoverTargetAction">popovertargetaction</a> to inform what we want to do (here we want to close the popover, but it could a toggle or an open behavior). Meaning without any JavaScript, we could easily pilot the way to interact with the popover.</p><p>From the above recording, the popover is centered in the page, and as we also centered the button, we don’t have a great experience.</p><p>Let’s use the <a href="https://developer.mozilla.org/en-US/docs/Web/CSS/Guides/Anchor_positioning/Using">anchor positioning API</a>:</p><pre>[popover=&#39;auto&#39;] {<br>  background: lightslategray;<br>  border-radius: 0.5rem;<br>  box-shadow: 1px 1px 3px lightslategray;<br>  border: none;<br>  text-align: center;<br><br>  /* Anchoring */<br>  bottom: calc(anchor(top) + 1rem);<br>  align-self: anchor-center;<br>  justify-self: anchor-center;<br><br>  /* Content */<br>  &amp; button {<br>    width: 10rem;<br>  }<br>}</pre><p>By using the anchor positioning API, we place our popover exactly where we want and so easily:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*KOb0WWeK2o-YP1iNokkRUg.gif" /><figcaption>The popover is now above our button</figcaption></figure><p>Now some little “hard” part: we want to apply some animation when we open and close the popover. Let’s modify the above CSS:</p><pre>[popover=&#39;auto&#39;] {<br>  background: lightslategray;<br>  border-radius: 0.5rem;<br>  box-shadow: 1px 1px 3px lightslategray;<br>  border: none;<br>  text-align: center;<br><br>  /* Anchoring */<br>  bottom: calc(anchor(top) + 1rem);<br>  align-self: anchor-center;<br>  justify-self: anchor-center;<br><br>  /* Animation */<br>  opacity: 1;<br>  transition: opacity .5s ease-in, bottom .5s ease-in, display .5s ease-in;<br>  transition-behavior: allow-discrete; /* For exit */<br><br>  &amp;:not(:popover-open) {<br>    bottom: anchor(bottom);<br>    opacity: 0;<br>  }<br><br>  @starting-style { /* Add properties to animate on opening */<br>    bottom: anchor(bottom);<br>    opacity: 0;<br>  }<br><br>  /* Content */<br>  &amp; button {<br>    width: 10rem;<br>  }<br>}</pre><p>So the popover should have a translation animation because we modify the <strong>bottom</strong> property between the two states (close/open) and also a fading animation.</p><p>We use <a href="https://developer.mozilla.org/en-US/docs/Web/CSS/Reference/At-rules/@starting-style">@starting-style</a> to define starting values for properties set on an element that you want to transition from when the element receives its first style update. If we don’t provide them, the CSS renderer will only the properties declared in the <strong>[popover=’auto’] </strong>part.</p><p>We define in the <strong>transition</strong> property the <strong>display</strong> property otherwise we will only an on/off effect due to the switch between <strong>display: none</strong> to <strong>display: block</strong>.</p><p>Last but not least, we declared <strong>transition-behavior: allow-discrete</strong> to inform we want a reverse transition (to apply when we close the popover).</p><p>So, everything is fine?</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*5F5mZW8rzki0J9Pq9jC0zg.gif" /><figcaption>Wait, what??</figcaption></figure><p>Weird indeed? The open transition works perfectly but the close transition did a weird effect.</p><p>Why? Simply look on the CSS state of the popover: we could see <strong>position-anchor: default.</strong></p><p>When we click on the button, there will be a “link” between the button and the popover. But the link vanished immediately when the popover switch to the “close” state.</p><p>Meaning we have to declare a “persistent” link.</p><p>We need to declare on the button an anchor name:</p><pre>&amp;[name=&#39;menu&#39;] {<br>  anchor-name: --menu;<br>}</pre><p>And then, we need to evolve the anchor positioning configuration of the popover:</p><pre>/* Anchoring */<br>bottom: calc(anchor(top) + 1rem);<br>align-self: anchor-center;<br>justify-self: anchor-center;<br>position-anchor: --menu; /* Important to have a smooth animation (and so we should declare anchor-name on the button */</pre><p>We could update the <strong>anchor(top)</strong> to <strong>anchor( — menu top) </strong>but we use <strong>positon-anchor</strong> to specify which anchor we are linked.</p><p>And now, we have a perfect animation:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*W09rm_BYrx0abCyUaefYyw.gif" /><figcaption>Neat animation</figcaption></figure><p>We should add some JavaScript to be informed when the buttons of the popover are clicked:</p><pre>const popoverElement = document.querySelector(&#39;[popover]&#39;);<br>popoverElement.addEventListener(<br>  &#39;click&#39;,<br>  (event) =&gt; {<br>    if (event.target.tagName === &#39;BUTTON&#39;) {<br>      setTimeout(() =&gt; {<br>        alert(`You have clicked on button: ${event.target.textContent}`);<br>      }, 150);<br>    }<br>  },<br>  false,<br>);</pre><p>That’s it!</p><p>Quickly and easily, we were able to create a button with a fancy menu using the Popover Api.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ba926690c414" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Use the dialog element as a drawer in your application]]></title>
            <link>https://medium.com/@rochejul/use-the-dialog-element-as-a-drawer-in-your-application-d960e90a6546?source=rss-67bfe57c1d45------2</link>
            <guid isPermaLink="false">https://medium.com/p/d960e90a6546</guid>
            <category><![CDATA[css]]></category>
            <category><![CDATA[html]]></category>
            <dc:creator><![CDATA[Julien Roche]]></dc:creator>
            <pubDate>Tue, 13 Jan 2026 12:57:54 GMT</pubDate>
            <atom:updated>2026-01-13T12:59:36.122Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*beclgRMvxDX-Clx3" /><figcaption>Let’s see how to use &lt;dialog&gt; like a navigation drawer</figcaption></figure><p>I made <a href="https://github.com/rochejul/medium-dialog-as-drawer">this tiny repository</a> (and a <a href="https://rochejul.github.io/medium-dialog-as-drawer/">live demo</a>) to share a concrete example of the <a href="https://developer.mozilla.org/en-US/docs/Web/HTML/Reference/Elements/dialog">&lt;dialog&gt;</a> element as <a href="https://mobbin.com/glossary/drawer">a drawer</a>.</p><p>This tag dialog is present from a while in our browser but recently faced to <a href="https://html.spec.whatwg.org/multipage/interactive-elements.html#the-dialog-element">some updates and improvements on the specification.</a></p><p>And we will see this is “easy” (when we understand how is rendered the &lt;dialog&gt; element) to create quickly a drawer element in our pages/applications.</p><p>First, let’s create a basic modal (note: we have dialogs and modals. A modal injects automatically a <a href="https://www.zorraquino.com/en/dictionary/ux/what-is-a-backdrop.html">backdrop</a> to prevent clicks):</p><pre>&lt;button commandfor=&quot;my-dialog&quot; command=&quot;show-modal&quot;&gt;Open a modal&lt;/button&gt;<br>&lt;dialog id=&quot;my-dialog&quot;&gt;<br>    &lt;p&gt;A very simple modal&lt;/p&gt;<br>    &lt;p&gt;<br>        &lt;i&gt;&lt;b&gt;(Opened with only HTML)&lt;/b&gt;&lt;/i&gt;<br>    &lt;/p&gt;<br>    &lt;form method=&quot;dialog&quot;&gt;<br>        &lt;button&gt;Close&lt;/button&gt;<br>    &lt;/form&gt;<br>&lt;/dialog&gt;</pre><p>With only HTML, we were able to create a simple modal with a backdrop.</p><figure><img alt="We could see a centered dialog with a backdrop element, preventing to interact with background elements" src="https://cdn-images-1.medium.com/max/1024/1*kzTAFbrI0lCwV6YuyyDpmg.png" /><figcaption>We could see a centered dialog with a backdrop element, preventing to interact with background elements</figcaption></figure><p>Here what looks like the user agent stylesheet for a modal:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/497/1*fxk0Ed-K_dIz2Ies7nLL9Q.png" /><figcaption>User agent stylesheet when we open a dialog as a modal</figcaption></figure><p>CSS brings some feature to customize the backdrop with the pseudo-class usage <a href="https://developer.mozilla.org/en-US/docs/Web/CSS/Reference/Selectors/::backdrop">::backdrop</a>. We could see the backdrop element as an element of the dialog.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/497/1*EgQtv7k4H4YOix5YQ0egdw.png" /><figcaption>We can see the additional backdrop element</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/497/1*9IGA2hhKzBE6UFt_sf1BKQ.png" /><figcaption>User agent stylesheet for the backdrop</figcaption></figure><p>For our use case, depending of the desired pattern, we could not need the usage of a modal drawer(e.g. a filtering drawer) but instead a non-modal drawer (e.g. navigation drawer).</p><p>Let’s define a navigation drawer. We have to evolve the above snippet in this way:</p><pre>&lt;button &gt;Open a modal&lt;/button&gt;<br>&lt;dialog id=&quot;my-dialog&quot;&gt;<br>    &lt;p&gt;A very simple modal&lt;/p&gt;<br>    &lt;form method=&quot;dialog&quot;&gt;<br>        &lt;button&gt;Close&lt;/button&gt;<br>    &lt;/form&gt;<br>&lt;/dialog&gt;</pre><pre>const buttonDialogElement = document.querySelector(&#39;button&#39;);<br>const drawerDialogElement = document.querySelector(&#39;dialog&#39;);<br><br>buttonDialogElement<br>  .addEventListener(&#39;click&#39;, () =&gt; drawerDialogElement.show());</pre><p>Why do we need to introduce JavaScript? Because at this time, <a href="https://pixicstudio.medium.com/html-invokers-the-coolest-api-you-arent-using-yet-e78c3ddee927">only the “show-modal” intent is available for HTML invokers</a>. And here we want more to customize the display of the backdrop: we want to allow the access of the background elements. Meaning we have to let through any user interactions. So rather to tweak the backdrop, we should simply not have it.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*oYqCgzNz1PuCSR3-MSh-Iw.png" /><figcaption>No more backdrop and the dialog is top-centered</figcaption></figure><p>Ok, we have our dialog, let’s have a look of the associated user agent stylesheet:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/492/1*mP41rk2oSnWmYJNh5Mmk6w.png" /><figcaption>User agent stylesheet for an usual dialog</figcaption></figure><p>We have a “position: absolute”, so naively, we will use properties “top” and “left” to display the navigation drawer on the left. However if we look closer, we could see on “width” and “height” properties, we have the <a href="https://developer.mozilla.org/en-US/docs/Web/CSS/Reference/Values/fit-content">fit-content</a> value that uses the content of the element to compute the size.</p><p>Meaning we have to define a height but also changes some margin:</p><pre>#my-dialog {<br>    height: 100%;<br>    margin-bottom: 0;<br>    margin-left: 0;<br>    margin-top: 0;<br>}</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*sBu1cDXI0O17tBdw7yxz5A.png" /><figcaption>The dialog appears now on the left, like a navigation drawer</figcaption></figure><p>Great, we have positioned the drawer.</p><p>Now let’s see how to define an open/close transition. A first implementation could generate the next snippet:</p><pre>#my-dialog {<br>    height: 100%;<br>    margin-bottom: 0;<br>    margin-left: 0;<br>    margin-top: 0;<br>    transition: margin-left 0.7s allow-discrete;<br>    width: 20rem;<br><br>    &amp;:not([open]) {<br>        margin-left: -23rem;<br>    }<br>}</pre><p>Let’s see what it looks like.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*L-ginJKetJeS24_K-r_svQ.gif" /><figcaption>Wait, no transition at all?</figcaption></figure><p>Damned, no transition at all.</p><p>Let’s look first on the opening part. In the CSS, we apply a negative margin to hide the dialog when the dialog is hidden.</p><p>However, this is the issue: such as <a href="https://developer.mozilla.org/en-US/docs/Web/HTML/Reference/Elements/template">&lt;template&gt;</a>, when the dialog is not open, there is no “styling” because the element is not rendered.</p><p>Meaning we should not only define a negative margin when the dialog is hidden, <a href="https://developer.mozilla.org/en-US/docs/Web/HTML/Reference/Elements/dialog#animating_dialogs">but also when we start the transition</a> (and more specifically, when the element receives for the time some styling). We need then of the CSS at-rule <a href="https://developer.mozilla.org/en-US/docs/Web/CSS/Reference/At-rules/@starting-style">@starting-style</a>:</p><pre>#my-dialog {<br>    height: 100%;<br>    margin-bottom: 0;<br>    margin-left: 0;<br>    margin-top: 0;<br>    transition: margin-left 0.7s allow-discrete;<br>    width: 20rem;<br><br>    &amp;:not([open]) {<br>        margin-left: -23rem;<br>    }<br><br>    @starting-style {<br>        margin-left: -23rem;<br>    }<br>}</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*aTclkI8NdqCuaLe_Er-ozg.gif" /><figcaption>Opening transition works now</figcaption></figure><p>Perfect, the open transition works. To understand of why the close transition doesn’t still work, let’s have a look on the browser’s default styling when the dialog is hidden:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/492/1*5Djk54-r6TfO_P5fpXW7gA.png" /></figure><p>We could see the &lt;dialog&gt; element has got a “none” display. <a href="https://developer.mozilla.org/en-US/docs/Web/HTML/Reference/Elements/dialog#animating_dialogs">Meaning the transition cannot be applied because when we close the dialog, the element is immediatelly hidden</a>.</p><p>We have to change the transition configuration to also include the “display” property:</p><pre>#my-dialog {<br>    height: 100%;<br>    margin-bottom: 0;<br>    margin-left: 0;<br>    margin-top: 0;<br>    transition:<br>        display 0.7s allow-discrete,<br>        margin-left 0.7s allow-discrete;<br>    width: 20rem;<br><br>    &amp;:not([open]) {<br>        margin-left: -23rem;<br>    }<br><br>    @starting-style {<br>        margin-left: -23rem;<br>    }<br>}</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*dsrLtbu5kmdFNVBHSVnedQ.gif" /><figcaption>The close transition works now.</figcaption></figure><p>Et voilà, we have our drawer with only using &lt;dialog&gt; element and very few CSS.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=d960e90a6546" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Are you familiar with Browser FingerPrint?]]></title>
            <link>https://medium.com/@rochejul/are-you-familiar-with-browser-fingerprint-8c7442f4e56e?source=rss-67bfe57c1d45------2</link>
            <guid isPermaLink="false">https://medium.com/p/8c7442f4e56e</guid>
            <category><![CDATA[browsers]]></category>
            <category><![CDATA[tracking]]></category>
            <category><![CDATA[web]]></category>
            <dc:creator><![CDATA[Julien Roche]]></dc:creator>
            <pubDate>Mon, 26 May 2025 19:17:24 GMT</pubDate>
            <atom:updated>2025-05-26T19:17:24.527Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*rpyoJYYxMC_hcKcEXdYvHg.avif" /><figcaption>Everyone is watching you</figcaption></figure><p>We, user, have got some naive behavior.</p><p>Like every day, we surf on the Internet, and suddenly, we see an offer on a marketplace where we are already register.</p><p>We decide to open our browser in incognito mode, to see what should be the price when we are not connected. Because, you know, in incognito, we are incognito, right?</p><p>… right?</p><p>This article is not about conspiracy theories: this is about some mechanisms used by mercantile website (or else) to detect you as an unique user and so to be sure to track you over websites (for most of the time advertisement).</p><p>To generate your browser fingerprint, it will use what we call the <a href="https://en.wikipedia.org/wiki/Canvas_fingerprinting">canvas fingerprinting</a>.</p><h3>Canvas fingerprinting principles</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*QoezUgoYx3rh_ejDjTW5dg.avif" /><figcaption>Your fingerprint</figcaption></figure><p>This approach is one over multiples to track online users (some collect the IP address, with the timezone, the installed plugins in your browser, etc…), but this is probably the most famous one (because it works so well).</p><p>The idea:</p><ul><li>Create an hidden canvas</li><li>Generate some words with all the police fonts installed in your machine</li><li>Generate some words with various colors</li><li>Extract the image with the JavaScript method <a href="https://developer.mozilla.org/en-US/docs/Web/API/HTMLCanvasElement/toDataURL">toDataUrl</a> to generate a <a href="https://developer.mozilla.org/en-US/docs/Web/URI/Reference/Schemes/data">data url</a> (and so have a base64 version of your canvas)</li><li>Store the data url (in the <a href="https://developer.mozilla.org/en-US/docs/Web/API/Window/localStorage">local storage</a>, for example).</li></ul><p>This will be our fingerprint.</p><p>But how could it be?</p><p>The principle is based on multiple layers:</p><ul><li>On your operating system (with the installed fonts and the various rendering bugs)</li><li>On your graphics hardware (with the display drivers, firmware version and the various rendering bugs due to the hardware or the drivers)</li><li>On your browser (with the browser engine, and also the various rendering bugs).</li></ul><p>This combination will help to identify you over websites, because multiple unique variables will affect the results (despite we sold the same CPU, this is not THE SAME: there could be some variations on performances, way to compute because of it also uses unique electronic components, with their owns small variations, etc…).</p><h3>Example with the tool fingerprintjs</h3><p>There is a maintained repository called <a href="https://github.com/fingerprintjs/fingerprintjs">fingerprintjs</a> (since 2015) to use the approach.</p><p>You have a website to test it: <a href="https://fingerprintjs.github.io/fingerprintjs/">https://fingerprintjs.github.io/fingerprintjs/</a></p><p>Let’s open it. We could see:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*QrHQmQFy4YXNefIZLtiFfQ.png" /><figcaption>The website with Chrome on my Ubuntu</figcaption></figure><p>The tool flags me with the identifier 35d490ba7e38ac2ae00c74074c84d5c0.</p><p>Let’s open the same website with the incognito mode:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*fbWcWFwzAnAPRlcmGjxVOg.png" /><figcaption>The same website with Chrome on my Ubuntu, but with the incognito mode</figcaption></figure><p>We could see the same identifier was generated.</p><p>We could also have a look on the metadata used to identify me as an uniq user;</p><ul><li>A generated canvas with some text/color</li><li>A generated canvas with a mixin of geometry forms and colors</li><li>Some screen information (frame rate, resolution, contrast, colorimetry, etc..)</li><li>Some basic system information (CPU, memory, OS, vendor, etc…)</li><li>Some advanced system information (fonts and default settings, some mathematics computations, etc…)</li><li>Some browser information (timezone, languages, plugins, WebGL configuration, browser’s option like the reduce motion, etc…)</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*A1E5Q3SqLNmik3VGt7ZVeA.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ZhugSHYvjOknKIT9vtkKiA.png" /><figcaption>The two generated canvases</figcaption></figure><p>These set of information will help to ensure you are the same visitor over web sites.</p><p>We are in an age where a lot of browsers use the same engine: <a href="https://www.chromium.org/blink/">Blink</a> for Chrome andEdge, <a href="https://www.chromium.org/chromium-projects/">Chromium</a> for Brave, Chromium, Opera, <a href="https://firefox-source-docs.mozilla.org/overview/gecko.html">Gecko</a> for Firefox and <a href="https://webkit.org/">WebKit</a> for the remaining browsers. It means potentially, depending of your habits, you could be track over browsers if you have similarities on the Web engine.</p><h3>Could we be not track?</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*DSyzqNt6-KezpTGGsa2rGw.avif" /><figcaption>Nice try</figcaption></figure><p>This is a complex question. In theory, we could in a short/medium term be untrackable… until a new parameter is find and added in the algorithm.</p><p>A nice website could help you to at least test frequently your situation, made by the Electronic Frontier Foundation (EFF) :</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*k9Csr1MYNVZKJQT8BmvZgg.png" /><figcaption><a href="https://coveryourtracks.eff.org/">https://coveryourtracks.eff.org/</a></figcaption></figure><p>With this tool, it will inspect your browser (and could test real entreprise solution) and will give you a report:</p><ul><li>With the current state: are you traceable?</li><li>With the report details for each detected parameter, how this is used in the tracking algorithm, etc…</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*-gj5Ghwbehmf4IrH05qoHQ.png" /><figcaption>Report details</figcaption></figure><h3>Conclusion</h3><p>This article doesn’t explain how the browser fingerprint works deeply behind the hood.</p><p>This is only to inform there are some tools to “easily” track visitors over websites and that H of incognito is not for hidden.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=8c7442f4e56e" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[NPM Workspaces: the good, the bad, the ugly, and an overview]]></title>
            <link>https://medium.com/@rochejul/npm-workspaces-e349da921d29?source=rss-67bfe57c1d45------2</link>
            <guid isPermaLink="false">https://medium.com/p/e349da921d29</guid>
            <category><![CDATA[workspace]]></category>
            <category><![CDATA[nodejs]]></category>
            <dc:creator><![CDATA[Julien Roche]]></dc:creator>
            <pubDate>Sun, 02 Feb 2025 13:25:11 GMT</pubDate>
            <atom:updated>2025-02-02T13:26:19.902Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*QcXWoD41h1xHp5lFNSHmSg.jpeg" /><figcaption>Organize the workspace of your project directly with NPM</figcaption></figure><p>As a developer, we have a need to split our code in various packages. This, for a large set of reasons:</p><ul><li>Separate concerns and responsibilities</li><li>The package could be reusable</li><li>Define appropriate tooling chain for the appropriate package</li><li>…</li></ul><p>In the <a href="https://nodejs.org/en">NodeJs</a>/<a href="https://www.npmjs.com/">NPM</a> ecosystem came a notion of “workspace”: in a <a href="https://monorepo.tools/">mono-repo</a>, we will define various NPM packages defined from a root package rather one.</p><p>The main benefits: we regroup a set of logic into the same repository and the versioning/dependencies over packages will be simplified.</p><p>This feature was a missing feature for a long time in NPM. That’s why some tooling appeared to deal with this need such as <a href="https://lerna.js.org/">LernaJS</a>, or some NPM alternative evolved to provide it such as <a href="https://classic.yarnpkg.com/lang/en/docs/workspaces/">Yarn</a>.</p><p>Now, we are please to be able to use <a href="https://docs.npmjs.com/cli/v7/using-npm/workspaces">this feature directly from NPM</a>.</p><p>Let’s have a look on it, and let’s discover what will be the good, the bad and the ugly parts. And we will create <a href="https://github.com/rochejul/medium-npm-workspaces">an example project</a> to illustrate the usage of NPM workspaces.</p><p>You will find the source code in the Github repository <a href="https://github.com/rochejul/medium-npm-workspaces">rochejul/medium-npm-workspaces</a> and each <a href="https://github.com/rochejul/medium-npm-workspaces/commits/main/">commits</a> reflects the step of the article.</p><h3>Define your root package</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*4wtYm5jTi6iO1fYcAYQFqA.jpeg" /><figcaption>Step by step, we will create our NPM workspace</figcaption></figure><p>First of all, we have to create a root package (with the usual <a href="https://docs.npmjs.com/cli/v8/commands/npm-init">npm command</a>):</p><pre>npm init</pre><p>We will call our project <strong>example</strong> and provide the right content:</p><pre>This utility will walk you through creating a package.json file.<br>It only covers the most common items, and tries to guess sensible defaults.<br><br>See `npm help init` for definitive documentation on these fields<br>and exactly what they do.<br><br>Use `npm install &lt;pkg&gt;` afterwards to install a package and<br>save it as a dependency in the package.json file.<br><br>Press ^C at any time to quit.<br>package name: (example) example<br>version: (1.0.0) <br>description: <br>entry point: (index.js) <br>test command: <br>git repository: <br>keywords: <br>author: <br>license: (ISC) <br>About to write to /xxx/example/package.json:<br><br>{<br>  &quot;name&quot;: &quot;example&quot;,<br>  &quot;version&quot;: &quot;1.0.0&quot;,<br>  &quot;main&quot;: &quot;index.js&quot;,<br>  &quot;scripts&quot;: {<br>    &quot;test&quot;: &quot;echo \&quot;Error: no test specified\&quot; &amp;&amp; exit 1&quot;<br>  },<br>  &quot;author&quot;: &quot;&quot;,<br>  &quot;license&quot;: &quot;ISC&quot;,<br>  &quot;description&quot;: &quot;&quot;<br>}<br><br><br>Is this OK? (yes) </pre><p>Our <strong>package.json </strong>file will be really simple:</p><pre>{<br>  &quot;name&quot;: &quot;example&quot;,<br>  &quot;version&quot;: &quot;1.0.0&quot;,<br>  &quot;main&quot;: &quot;index.js&quot;,<br>  &quot;scripts&quot;: {<br>    &quot;test&quot;: &quot;echo \&quot;Error: no test specified\&quot; &amp;&amp; exit 1&quot;<br>  },<br>  &quot;author&quot;: &quot;&quot;,<br>  &quot;license&quot;: &quot;ISC&quot;,<br>  &quot;description&quot;: &quot;&quot;<br>}</pre><p>Let’s also run the install command to generate the <strong>package-lock.json</strong> file:</p><pre>npm install</pre><p>Now, let’s have a look of the <a href="https://docs.npmjs.com/cli/v7/using-npm/workspaces">documentation</a>: we have to declare in the <strong>package.json </strong>file a property called <strong>workspaces.</strong> This property is an array where you provide a path of folders which contains an NPM package.</p><p>You could use <a href="https://www.devx.com/terms/globbing/">globbing</a> to find dynamically the packages. You could have some details with <a href="https://github.com/pnpm/pnpm/tree/main/fs/find-packages">@pnpm/fs.find-packages</a> which reproduces the logic to find programatically the packages of a NPM workspace.</p><p>Then your <strong>package.json </strong>file could be replaced from:</p><pre>{<br>  &quot;name&quot;: &quot;example&quot;,<br>  &quot;version&quot;: &quot;1.0.0&quot;,<br>  &quot;main&quot;: &quot;index.js&quot;,<br>  &quot;workspaces&quot;: [<br>    &quot;./packages/moduleA&quot;,<br>    &quot;./packages/moduleB&quot;<br>  ],<br>  &quot;scripts&quot;: {<br>    &quot;test&quot;: &quot;echo \&quot;Error: no test specified\&quot; &amp;&amp; exit 1&quot;<br>  },<br>  &quot;author&quot;: &quot;&quot;,<br>  &quot;license&quot;: &quot;ISC&quot;,<br>  &quot;description&quot;: &quot;&quot;<br>}</pre><p>To:</p><pre>{<br>  &quot;name&quot;: &quot;example&quot;,<br>  &quot;version&quot;: &quot;1.0.0&quot;,<br>  &quot;main&quot;: &quot;index.js&quot;,<br>  &quot;workspaces&quot;: [<br>    &quot;./packages/*&quot;<br>  ],<br>  &quot;scripts&quot;: {<br>    &quot;test&quot;: &quot;echo \&quot;Error: no test specified\&quot; &amp;&amp; exit 1&quot;<br>  },<br>  &quot;author&quot;: &quot;&quot;,<br>  &quot;license&quot;: &quot;ISC&quot;,<br>  &quot;description&quot;: &quot;&quot;<br>}</pre><p>We will also prepare the <strong>packages</strong> folder:</p><pre>mkdir packages<br>touch ./packages/.gitkeep</pre><h3>Root package: public or not public?</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*dlw-XO-QDUyZPYjrEanRRA.jpeg" /><figcaption>This is the question</figcaption></figure><p>What is the purpose of our workspace?</p><p>Is it a set of tools like <a href="https://jestjs.io/fr/">Jest</a>? Then probably the root package should be <a href="https://github.com/jestjs/jest/blob/5f26f09cc5bbf0d82faa6f91f77283db7246cfbb/package.json#L3">declared as private</a> and then only packages could be <a href="https://github.com/jestjs/jest/blob/5f26f09cc5bbf0d82faa6f91f77283db7246cfbb/packages/jest-cli/package.json#L2-L6">declared as public</a>.</p><p>Or is a tool like <a href="https://www.npmjs.com/package/npm">NPM cli</a>? Then the root package should be <a href="https://github.com/npm/cli/blob/dc31c1bdc6658ab69554adcf2988ee99a615c409/package.json#L2-L11">declared as public</a> and packages could be also <a href="https://github.com/npm/cli/blob/dc31c1bdc6658ab69554adcf2988ee99a615c409/workspaces/config/package.json#L2-L7">declared as public</a> (and then be used as a reusable part of the tool).</p><p>However, we have to be careful of the name of the root package (and of the sub-packages). Indeed, the sub-packages live as “standalone” packages. Meaning there is no notion of naming composition based on the root package: each package should declared his full name.</p><p>Usually, the root package has got the name of project and the sub-packages uses some organization name. For example, the root package could be called <strong>example</strong> and the sub-packages could be named like <strong>@example/core</strong>.</p><p>In our use case, let’s make it private and we will also set the type of the root package as a module:</p><pre>{<br>  &quot;name&quot;: &quot;example&quot;,<br>  &quot;version&quot;: &quot;1.0.0&quot;,<br>  &quot;main&quot;: &quot;index.js&quot;,<br>  &quot;type&quot;: &quot;module&quot;,<br>  &quot;private&quot;: &quot;true&quot;,<br>  &quot;workspaces&quot;: [<br>    &quot;./packages/*&quot;<br>  ],<br>  &quot;scripts&quot;: {<br>    &quot;start&quot;: &quot;node index.js&quot;<br>  },<br>  &quot;author&quot;: &quot;&quot;,<br>  &quot;license&quot;: &quot;ISC&quot;,<br>  &quot;description&quot;: &quot;&quot;<br>}</pre><p>And let’s create a simple<strong> index.js</strong> file:</p><pre>console.info(`I&#39;m the root package`);</pre><p>Obviously, we are in a NPM project, so we should not forget to provide a <a href="https://github.com/rochejul/medium-npm-workspaces/blob/main/.gitattributes">.gitattributes</a> and a <a href="https://github.com/rochejul/medium-npm-workspaces/blob/main/.gitignore">.gitignore</a> files.</p><h3>Organize your workspace</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*faaxaK0ryXPSgobDEVMAZQ.jpeg" /><figcaption>Everything at the right place</figcaption></figure><p>Let’s have a deeper look on the <a href="https://docs.npmjs.com/cli/v8/commands/npm-init">npm init command</a>, we could see some options related to workspaces.</p><p>And as an overall aspect, most of the npm commands have at least these three workspace parameters:</p><ul><li><strong>workspace</strong>: apply the command on one package</li><li><strong>workspaces</strong>: apply the command on all packages</li><li><strong>include-workspace-root</strong>: apply the command also for the root package (only if we used one of the two previous options)</li></ul><p>Obviously, some commands have more (we will see later in the article)</p><p>For the init command, <strong>workspaces</strong> and <strong>include-workspace-root</strong> are useful only if we have created manually the hierarchy and the <strong>package.json </strong>files.</p><p>In our case, we want to create step by step our workspace, so we will use the workspace option, where we define the name and the path of the new package.</p><pre>npm init -w ./packages/module-a</pre><p>This is time to remember the previous section about public/private packages and naming. In our case, the package will be public and their name will start with <strong>@example/</strong></p><pre>package name: (module-a) @example/module-a<br>version: (1.0.0) <br>description: The first module<br>entry point: (index.js) <br>test command: <br>git repository: <br>keywords: <br>author: <br>license: (ISC) <br>About to write to /xxx/example/packages/module-a/package.json:<br><br>{<br>  &quot;name&quot;: &quot;@example/module-a&quot;,<br>  &quot;version&quot;: &quot;1.0.0&quot;,<br>  &quot;description&quot;: &quot;The first module&quot;,<br>  &quot;main&quot;: &quot;index.js&quot;,<br>  &quot;scripts&quot;: {<br>    &quot;test&quot;: &quot;echo \&quot;Error: no test specified\&quot; &amp;&amp; exit 1&quot;<br>  },<br>  &quot;author&quot;: &quot;&quot;,<br>  &quot;license&quot;: &quot;ISC&quot;<br>}<br><br><br>Is this OK? (yes) yes<br><br><br>added 1 package in 24s</pre><p>We will update the generated <strong>package.json</strong> file to specify we will use the module type.</p><p>Let’s have a look in our workspace:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/859/1*9WAoixwo1Am_dcex74c4Gw.png" /><figcaption>Only one package-lock.json?</figcaption></figure><p>First, we have only p<strong>ackage-lock.json</strong> file. In a workspace, we will always one and only one<strong> package-lock.json</strong>, at the root level.</p><p>At this time, we also have one <strong>node_modules</strong> but if we install dependencies directly on packages with some different versions, we could find multiple <strong>node_modules</strong>.</p><p>Another thing: we could see our created package in <strong>node_modules</strong>!!</p><p>If we look in the <strong>package-lock.json</strong>, we will understand why:</p><pre>{<br>  &quot;name&quot;: &quot;example&quot;,<br>  &quot;version&quot;: &quot;1.0.0&quot;,<br>  &quot;lockfileVersion&quot;: 3,<br>  &quot;requires&quot;: true,<br>  &quot;packages&quot;: {<br>    &quot;&quot;: {<br>      &quot;name&quot;: &quot;example&quot;,<br>      &quot;version&quot;: &quot;1.0.0&quot;,<br>      &quot;license&quot;: &quot;ISC&quot;,<br>      &quot;workspaces&quot;: [<br>        &quot;./packages/*&quot;<br>      ]<br>    },<br>    &quot;node_modules/@example/module-a&quot;: {<br>      &quot;resolved&quot;: &quot;packages/module-a&quot;,<br>      &quot;link&quot;: true<br>    },<br>    &quot;packages/module-a&quot;: {<br>      &quot;version&quot;: &quot;1.0.0&quot;,<br>      &quot;license&quot;: &quot;ISC&quot;<br>    }<br>  }<br>}</pre><p>The package a symbolic link (behind the hood, the init command used a <a href="https://docs.npmjs.com/cli/v8/commands/npm-link">npm link command</a>). This could be verified with the following command:</p><pre>ls -la node_modules/@example/module-a<br>lrwxrwxrwx 1 julien-roche julien-roche 23 Jan 25 15:38 node_modules/@example/module-a -&gt; ../../packages/module-a</pre><p>The reason? Because we could access our package in the other packages or at the root level. And so import them easily.</p><p>Let’s check that. In <strong>@example/module-a’</strong>s <strong>index.js</strong> file, we will declare:</p><pre>export function sum(num1, num2) {<br>  return num1 + num2;<br>}</pre><p>Let’s create a <strong>@example/module-b </strong>module and with the following <strong>index.js</strong> file:</p><pre>export function sub(num1, num2) {<br>  return num1 - num2;<br>}</pre><p>Now, at the root package, let’s modify the <strong>index.js</strong> file in this way:</p><pre>import { sum } from &quot;@example/module-a&quot;;<br>import { sub } from &quot;@example/module-b&quot;;<br><br>const result = sub(sum(25, 42), 7);<br>console.info(&quot;Result:&quot;, result);</pre><p>And run the script:</p><pre>npm start<br><br>&gt; example@1.0.0 start<br>&gt; node index.js<br><br>Result: 60</pre><p>We are close to have the barebone of our workspace. Indeed, it works because the sub-packages are symboly linked. We have to import them officially in root-package’s <strong>package.json</strong>.</p><p>Let’s achieve this first step by running this command:</p><pre>npm install --save-exact @example/module-a @example/module-b</pre><p>Tada, we have our workspace ready now.</p><h3>What about package inter-dependencies?</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*9n6P0wMVDTZc9U6zDtkQEw.jpeg" /><figcaption>Use a sub-package as a dependency of another sub-package</figcaption></figure><p>On the previous section, we saw we were able to add at the root-level package as dependencies sub-packages.</p><p>This is also possible between packages obviously. We only need on the npm install command to use the workspace option to target the expected package of the workspace.</p><p>Here an example:</p><pre>npm install --save @npmversion/core --workspace=packages/cli</pre><p>The only rule to respect: the package should be public and so publishable.</p><p>Indeed, locally you will never have trouble (though…).</p><p>But when an end-user will grab your package A, if it relies to package B of your workspace which is private, it means it cannot be published and so the end-user cannot download it and use your module A.</p><p>So what about private packages.</p><h3>Private packages in my workspace</h3><figure><img alt="" src="https://cdn-images-1.medium.com/proxy/1*aGa9Rbz0JNL39-iRS29OGg.jpeg" /></figure><p>We have to use a NPM feature called <a href="https://nodejs.org/en/blog/npm/peer-dependencies">peer dependencies</a>. The main idea: you declare you need a package but <a href="https://docs.npmjs.com/cli/v7/configuring-npm/package-json#peerdependencies">the end-user has got the responsibility to install</a>.</p><p>However we should be carefull now since NPM v7, we have to also declare a <a href="https://docs.npmjs.com/cli/v7/configuring-npm/package-json#peerdependencies">peerDependenciesMeta</a> to really specify this is an optional package (otherwise NPM will do some versioning checkings and so inspect the NPM registry if the package is installed, etc…).</p><p>What it involves?</p><p>It means private packages are possible in NPM workspaces, but:</p><ul><li>They are not publishable</li><li>They are available only “locally”</li><li>They should be used for tooling aspect</li></ul><p>At the end, it means the private packages should be used a <a href="https://docs.npmjs.com/cli/v7/configuring-npm/package-json#devdependencies">devDependencies</a> of your package.</p><p>Let’s create a sub-package for testing purpose.</p><p>Let’s run the command:</p><pre>npm init -w ./packages/jest-utils</pre><p>We should modify the generated <strong>package.json </strong>file to specify the package’s type and visibility (private).</p><p>We will install some <a href="https://jestjs.io/fr/">Jest</a> packages:</p><pre>npm install -w ./packages/jest-utils --save --save-exact jest@29.7.0 @types/jest@29.5.1</pre><p>The <strong>package.json</strong> file should look</p><pre>{<br>  &quot;name&quot;: &quot;@example/jest-utils&quot;,<br>  &quot;version&quot;: &quot;1.0.0&quot;,<br>  &quot;type&quot;: &quot;module&quot;,<br>  &quot;private&quot;: &quot;true&quot;,<br>  &quot;description&quot;: &quot;Utility package around jest&quot;,<br>  &quot;main&quot;: &quot;index.js&quot;,<br>  &quot;author&quot;: &quot;&quot;,<br>  &quot;license&quot;: &quot;ISC&quot;,<br>  &quot;dependencies&quot;: {<br>    &quot;@types/jest&quot;: &quot;29.5.14&quot;,<br>    &quot;jest&quot;: &quot;29.7.0&quot;<br>  }<br>}</pre><p>In the <strong>index.js</strong> file, let’s create a simple matcher (to easily check any kind of object as a plain object):</p><pre>import { expect } from &quot;@jest/globals&quot;;<br><br>function plainObject(object) {<br>  return JSON.parse(JSON.stringify(object));<br>}<br><br>expect.extend({<br>  toMatchPlainObject(received, expected) {<br>    const receivedPlainObject = plainObject(received);<br>    const pass = this.equals(receivedPlainObject, expected, [], true);<br><br>    if (pass) {<br>      return {<br>        message: () =&gt;<br>          `Expected: ${this.utils.printExpected(<br>            expected<br>          )}\nReceived: ${this.utils.printReceived(receivedPlainObject)}`,<br>        pass: true,<br>      };<br>    }<br><br>    return {<br>      message: () =&gt;<br>        `Expected: ${this.utils.printExpected(<br>          expected<br>        )}\nReceived: ${this.utils.printReceived(<br>          receivedPlainObject<br>        )}\n\n${this.utils.diff(expected, receivedPlainObject)}`,<br>      pass: false,<br>    };<br>  },<br>});</pre><p>Now let’s use it in the module-a:</p><pre>npm install -w ./packages/module-a --save-peer --save-exact @example/jest-utils</pre><p>We also to declare the <strong>peerDependenciesMeta</strong> property, and should we have in the<strong> package.json</strong> of the module-a package:</p><pre>{<br>  &quot;name&quot;: &quot;@example/module-a&quot;,<br>  &quot;version&quot;: &quot;1.0.0&quot;,<br>  &quot;description&quot;: &quot;The first module&quot;,<br>  &quot;main&quot;: &quot;index.js&quot;,<br>  &quot;type&quot;: &quot;module&quot;,<br>  &quot;scripts&quot;: {<br>    &quot;test&quot;: &quot;echo \&quot;Error: no test specified\&quot; &amp;&amp; exit 1&quot;<br>  },<br>  &quot;author&quot;: &quot;&quot;,<br>  &quot;license&quot;: &quot;ISC&quot;,<br>  &quot;peerDependencies&quot;: {<br>    &quot;@example/jest-utils&quot;: &quot;1.0.0&quot;<br>  },<br>  &quot;peerDependenciesMeta&quot;: {<br>    &quot;@example/jest-utils&quot;: {<br>      &quot;optional&quot;: true<br>    }<br>  }<br>}</pre><p>To reflect the changes related to <strong>peerDependenciesMeta</strong>, you should also run a <a href="https://docs.npmjs.com/cli/v9/commands/npm-prune">pruning command</a>:</p><pre>npm prune</pre><p>Congratulations, you declared a private package in your workspace and use it in another one.</p><p>Little disclaimer, we will see on the “<strong>Time to bump our version</strong>” chapter this is recommended to always use a wildcard rather a strict version:</p><pre>{<br>  &quot;name&quot;: &quot;@example/module-a&quot;,<br>  &quot;version&quot;: &quot;1.0.0&quot;,<br>  &quot;description&quot;: &quot;The first module&quot;,<br>  &quot;main&quot;: &quot;index.js&quot;,<br>  &quot;type&quot;: &quot;module&quot;,<br>  &quot;scripts&quot;: {<br>    &quot;dev:linting&quot;: &quot;eslint --config=eslint.config.js&quot;,<br>    &quot;test&quot;: &quot;NODE_OPTIONS=\&quot;$NODE_OPTIONS --enable-source-maps --experimental-vm-modules\&quot; npx jest --no-cache --config jest.config.json&quot;,<br>    &quot;test:coverage&quot;: &quot;npm run test -- --coverage&quot;<br>  },<br>  &quot;//&quot;: {<br>    &quot;dev:linting&quot;: &quot;Lint files&quot;,<br>    &quot;test&quot;: &quot;Run Jest tests&quot;,<br>    &quot;test:coverage&quot;: &quot;Run Jest tests and collect coverage&quot;<br>  },<br>  &quot;author&quot;: &quot;&quot;,<br>  &quot;license&quot;: &quot;ISC&quot;,<br>  &quot;peerDependencies&quot;: {<br>    &quot;@example/jest-utils&quot;: &quot;*&quot;<br>  },<br>  &quot;peerDependenciesMeta&quot;: {<br>    &quot;@example/jest-utils&quot;: {<br>      &quot;optional&quot;: true<br>    }<br>  }<br>}</pre><h3>Let’s run some tests (and some other commands)</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*sG3GRnw4wyPIu32b2M14uw.jpeg" /><figcaption>Wait! Did I miss something?</figcaption></figure><p>On a NPM project, we usually use <a href="https://docs.npmjs.com/cli/v10/commands/npm-run-script/">npm run-script</a> to have some commands for the dev tool chain (running tests, styling, generate docs, etc…) and we also use it for a CI perspective.</p><p>Let’s enable some tests and see how it works with a NPM workspace.</p><p>In the module-a package, we will create the <strong>jest.config.json </strong>file:</p><pre>{<br>  &quot;clearMocks&quot;: true,<br>  &quot;collectCoverage&quot;: true,<br>  &quot;collectCoverageFrom&quot;: [&quot;index.js&quot;],<br>  &quot;coverageThreshold&quot;: {<br>    &quot;global&quot;: {<br>      &quot;lines&quot;: 90,<br>      &quot;statements&quot;: 90<br>    }<br>  },<br>  &quot;coverageDirectory&quot;: &quot;&lt;rootDir&gt;/coverage&quot;,<br>  &quot;moduleFileExtensions&quot;: [&quot;mjs&quot;, &quot;js&quot;],<br>  &quot;setupFilesAfterEnv&quot;: [&quot;&lt;rootDir&gt;/test/jest.setup.js&quot;],<br>  &quot;testEnvironment&quot;: &quot;node&quot;,<br>  &quot;testRegex&quot;: [&quot;.*test\\.js$&quot;],<br>  &quot;transform&quot;: {},<br>  &quot;verbose&quot;: true<br>}</pre><p>We prepare the <strong>test npm run-script</strong> in module-a’s <strong>package.json</strong> file (NB: we use the module type for our package, so we have to declare some flags for Jest):</p><pre>{<br>  &quot;name&quot;: &quot;@example/module-a&quot;,<br>  &quot;version&quot;: &quot;1.0.0&quot;,<br>  &quot;description&quot;: &quot;The first module&quot;,<br>  &quot;main&quot;: &quot;index.js&quot;,<br>  &quot;type&quot;: &quot;module&quot;,<br>  &quot;scripts&quot;: {<br>    &quot;test&quot;: &quot;NODE_OPTIONS=\&quot;$NODE_OPTIONS --enable-source-maps --experimental-vm-modules\&quot; npx jest --no-cache --config jest.config.json&quot;,<br>    &quot;test:coverage&quot;: &quot;npm run test -- --coverage&quot;<br>  },<br>  &quot;//&quot;: {<br>    &quot;test&quot;: &quot;Run Jest tests&quot;,<br>    &quot;test:coverage&quot;: &quot;Run Jest tests and collect coverage&quot;<br>  },<br>  &quot;author&quot;: &quot;&quot;,<br>  &quot;license&quot;: &quot;ISC&quot;,<br>  &quot;peerDependencies&quot;: {<br>    &quot;@example/jest-utils&quot;: &quot;1.0.0&quot;<br>  },<br>  &quot;peerDependenciesMeta&quot;: {<br>    &quot;@example/jest-utils&quot;: {<br>      &quot;optional&quot;: true<br>    }<br>  }<br>}</pre><p>And create a very very very basic test (where we use the custom matcher <strong>toMatchPlainObject</strong> we created in the private package <strong>jest-utils</strong>):</p><pre>import { describe, test, expect } from &quot;@jest/globals&quot;;<br>import &quot;@example/jest-utils&quot;;<br>import { sum } from &quot;../index.js&quot;;<br><br>describe(&quot;@example/module-a - sum&quot;, () =&gt; {<br>  test(&quot;we should sum the two parameters&quot;, () =&gt; {<br>    // Arrange<br>    const leftOperand = 25;<br>    const righOperand = 37;<br>    const expected = { sum: 62 };<br><br>    // Act<br>    const actual = sum(leftOperand, righOperand);<br><br>    // Assert<br>    expect({ sum: actual }).toMatchPlainObject(expected);<br>  });<br>});</pre><p>If we run the following commands:</p><pre>cd ./packages/module-a<br>npm test</pre><p>We will have as output:</p><pre>&gt; @example/module-a@1.0.0 test<br>&gt; NODE_OPTIONS=&quot;$NODE_OPTIONS --enable-source-maps --experimental-vm-modules&quot; npx jest --no-cache --config jest.config.json<br><br>(node:6185) ExperimentalWarning: VM Modules is an experimental feature and might change at any time<br>(Use `node --trace-warnings ...` to show where the warning was created)<br> PASS  test/index.test.js<br>  @example/module-a - sum<br>    ✓ we should sum the two parameters (3 ms)<br><br>----------|---------|----------|---------|---------|-------------------<br>File      | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s <br>----------|---------|----------|---------|---------|-------------------<br>All files |     100 |      100 |     100 |     100 |                   <br> index.js |     100 |      100 |     100 |     100 |                   <br>----------|---------|----------|---------|---------|-------------------<br>Test Suites: 1 passed, 1 total<br>Tests:       1 passed, 1 total<br>Snapshots:   0 total<br>Time:        0.64 s<br>Ran all test suites.</pre><p>So the test works fine and it proves the imported private package works too. But we needed to execute the command within the package scope.</p><p>How to run it from the root-level package?</p><p>We need to use the <a href="https://docs.npmjs.com/cli/v10/commands/npm-run-script#workspace">workspace option</a>. If we adapt the root-level <strong>package.json</strong> file:</p><pre>{<br>  &quot;name&quot;: &quot;example&quot;,<br>  &quot;version&quot;: &quot;1.0.0&quot;,<br>  &quot;main&quot;: &quot;index.js&quot;,<br>  &quot;type&quot;: &quot;module&quot;,<br>  &quot;private&quot;: &quot;true&quot;,<br>  &quot;workspaces&quot;: [<br>    &quot;./packages/*&quot;<br>  ],<br>  &quot;scripts&quot;: {<br>    &quot;start&quot;: &quot;node index.js&quot;,<br>    &quot;test:module-a&quot;: &quot;npm test --workspace=./packages/module-a&quot;<br>  },<br>  &quot;//&quot;: {<br>    &quot;test:module-a&quot;: &quot;Run tests only for @example/module-a&quot;<br>  },<br>  &quot;author&quot;: &quot;&quot;,<br>  &quot;license&quot;: &quot;ISC&quot;,<br>  &quot;description&quot;: &quot;&quot;,<br>  &quot;dependencies&quot;: {<br>    &quot;@example/module-a&quot;: &quot;1.0.0&quot;,<br>    &quot;@example/module-b&quot;: &quot;1.0.0&quot;<br>  }<br>}</pre><p>Then:</p><pre>~/dev/github/example$ npm run test:module-a<br><br>&gt; example@1.0.0 test:module-a<br>&gt; npm test --workspace=./packages/module-a<br><br><br>&gt; @example/module-a@1.0.0 test<br>&gt; NODE_OPTIONS=&quot;$NODE_OPTIONS --enable-source-maps --experimental-vm-modules&quot; npx jest --no-cache --config jest.config.json<br><br>(node:6509) ExperimentalWarning: VM Modules is an experimental feature and might change at any time<br>(Use `node --trace-warnings ...` to show where the warning was created)<br> PASS  test/index.test.js<br>  @example/module-a - sum<br>    ✓ we should sum the two parameters (4 ms)<br><br>----------|---------|----------|---------|---------|-------------------<br>File      | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s <br>----------|---------|----------|---------|---------|-------------------<br>All files |     100 |      100 |     100 |     100 |                   <br> index.js |     100 |      100 |     100 |     100 |                   <br>----------|---------|----------|---------|---------|-------------------<br>Test Suites: 1 passed, 1 total<br>Tests:       1 passed, 1 total<br>Snapshots:   0 total<br>Time:        0.628 s<br>Ran all test suites.</pre><p>Great, but let’s see to generalize it with one command for all packages with the option <a href="https://docs.npmjs.com/cli/v10/commands/npm-run-script#workspaces">workspaces</a>:</p><pre>{<br>  &quot;name&quot;: &quot;example&quot;,<br>  &quot;version&quot;: &quot;1.0.0&quot;,<br>  &quot;main&quot;: &quot;index.js&quot;,<br>  &quot;type&quot;: &quot;module&quot;,<br>  &quot;private&quot;: &quot;true&quot;,<br>  &quot;workspaces&quot;: [<br>    &quot;./packages/*&quot;<br>  ],<br>  &quot;scripts&quot;: {<br>    &quot;start&quot;: &quot;node index.js&quot;,<br>    &quot;test&quot;: &quot;npm run test --workspaces&quot;,<br>    &quot;test:module-a&quot;: &quot;npm test --workspace=./packages/module-a&quot;<br>  },<br>  &quot;//&quot;: {<br>    &quot;test&quot;: &quot;Run tests for all sub-packages&quot;,<br>    &quot;test:module-a&quot;: &quot;Run tests only for @example/module-a&quot;<br>  },<br>  &quot;author&quot;: &quot;&quot;,<br>  &quot;license&quot;: &quot;ISC&quot;,<br>  &quot;description&quot;: &quot;&quot;,<br>  &quot;dependencies&quot;: {<br>    &quot;@example/module-a&quot;: &quot;1.0.0&quot;,<br>    &quot;@example/module-b&quot;: &quot;1.0.0&quot;<br>  }<br>}</pre><p>If we run the command, we could see a failure:</p><pre>~/dev/github/example$ npm test<br><br>&gt; example@1.0.0 test<br>&gt; npm run test --workspaces<br><br>npm error Lifecycle script `test` failed with error:<br>npm error Error: Missing script: &quot;test&quot;<br><br>To see a list of scripts, run:<br>  npm run<br>npm error   in workspace: @example/jest-utils@1.0.0<br>npm error   at location: /xxx/dev/github/example/packages/jest-utils<br><br>&gt; @example/module-a@1.0.0 test<br>&gt; NODE_OPTIONS=&quot;$NODE_OPTIONS --enable-source-maps --experimental-vm-modules&quot; npx jest --no-cache --config jest.config.json<br><br>(node:7486) ExperimentalWarning: VM Modules is an experimental feature and might change at any time<br>(Use `node --trace-warnings ...` to show where the warning was created)<br> PASS  test/index.test.js<br>  @example/module-a - sum<br>    ✓ we should sum the two parameters (3 ms)<br><br>----------|---------|----------|---------|---------|-------------------<br>File      | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s <br>----------|---------|----------|---------|---------|-------------------<br>All files |     100 |      100 |     100 |     100 |                   <br> index.js |     100 |      100 |     100 |     100 |                   <br>----------|---------|----------|---------|---------|-------------------<br>Test Suites: 1 passed, 1 total<br>Tests:       1 passed, 1 total<br>Snapshots:   0 total<br>Time:        0.704 s<br>Ran all test suites.<br>npm error Lifecycle script `test` failed with error:<br>npm error Error: Missing script: &quot;test&quot;<br><br>To see a list of scripts, run:<br>  npm run<br>npm error   in workspace: @example/module-b@1.0.0<br>npm error   at location: /xxx/dev/github/example/packages/module-b</pre><p>This is logic: we never declare any tests <em>yet</em> in module-b. Hopefully, we could use the <a href="https://docs.npmjs.com/cli/v10/commands/npm-run-script#if-present">if-present option</a> which ignore packages where the command is not declared:</p><pre>{<br>  &quot;name&quot;: &quot;example&quot;,<br>  &quot;version&quot;: &quot;1.0.0&quot;,<br>  &quot;main&quot;: &quot;index.js&quot;,<br>  &quot;type&quot;: &quot;module&quot;,<br>  &quot;private&quot;: &quot;true&quot;,<br>  &quot;workspaces&quot;: [<br>    &quot;./packages/*&quot;<br>  ],<br>  &quot;scripts&quot;: {<br>    &quot;start&quot;: &quot;node index.js&quot;,<br>    &quot;test&quot;: &quot;npm run test --workspaces --if-present&quot;,<br>    &quot;test:module-a&quot;: &quot;npm test --workspace=./packages/module-a&quot;<br>  },<br>  &quot;//&quot;: {<br>    &quot;test&quot;: &quot;Run tests for all sub-packages&quot;,<br>    &quot;test:module-a&quot;: &quot;Run tests only for @example/module-a&quot;<br>  },<br>  &quot;author&quot;: &quot;&quot;,<br>  &quot;license&quot;: &quot;ISC&quot;,<br>  &quot;description&quot;: &quot;&quot;,<br>  &quot;dependencies&quot;: {<br>    &quot;@example/module-a&quot;: &quot;1.0.0&quot;,<br>    &quot;@example/module-b&quot;: &quot;1.0.0&quot;<br>  }<br>}</pre><p>Then, the <strong>npm test</strong> command works fine:</p><pre>~/dev/github/example$ npm test<br><br>&gt; example@1.0.0 test<br>&gt; npm run test --workspaces --if-present<br><br><br>&gt; @example/module-a@1.0.0 test<br>&gt; NODE_OPTIONS=&quot;$NODE_OPTIONS --enable-source-maps --experimental-vm-modules&quot; npx jest --no-cache --config jest.config.json<br><br>(node:7244) ExperimentalWarning: VM Modules is an experimental feature and might change at any time<br>(Use `node --trace-warnings ...` to show where the warning was created)<br> PASS  test/index.test.js<br>  @example/module-a - sum<br>    ✓ we should sum the two parameters (4 ms)<br><br>----------|---------|----------|---------|---------|-------------------<br>File      | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s <br>----------|---------|----------|---------|---------|-------------------<br>All files |     100 |      100 |     100 |     100 |                   <br> index.js |     100 |      100 |     100 |     100 |                   <br>----------|---------|----------|---------|---------|-------------------<br>Test Suites: 1 passed, 1 total<br>Tests:       1 passed, 1 total<br>Snapshots:   0 total<br>Time:        0.658 s<br>Ran all test suites.</pre><p>We could see we executed the test commands only in sub-packages. If we want to execute it also on the root-level, we could use t<a href="https://docs.npmjs.com/cli/v10/commands/npm-run-script#include-workspace-root">he include-workspace-root option</a>, but <em>in our case, we shouldn’t otherwise we will have an infinite</em> <em>loop</em> -_-’.</p><h3>What about publishing our packages?</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*lI4YMeHnpDZQHqFWoY0iFA.jpeg" /><figcaption>Publishing on NPM registry</figcaption></figure><p>We have to use the <a href="https://docs.npmjs.com/cli/v8/commands/npm-publish">npm publish</a> command. Similarly, we could use the various workspaces options and we could have a command such as:</p><pre>npm publish --workspaces --include-workspace-root</pre><p>The above commands:</p><ul><li>Will try to publish <em>any sub-packages</em> (if this is specified as <em>private</em>, the package will be ignored)</li><li>Will try to publish the root level package (if this is specified as <em>private</em>, the package will be ignored)</li></ul><p>At the end, this part of the journey is really a sample one and the root-level <strong>package.json</strong> file looks like now:</p><pre>{<br>  &quot;name&quot;: &quot;example&quot;,<br>  &quot;version&quot;: &quot;1.0.0&quot;,<br>  &quot;main&quot;: &quot;index.js&quot;,<br>  &quot;type&quot;: &quot;module&quot;,<br>  &quot;private&quot;: &quot;true&quot;,<br>  &quot;workspaces&quot;: [<br>    &quot;./packages/*&quot;<br>  ],<br>  &quot;scripts&quot;: {<br>    &quot;dev:publish&quot;: &quot;npm publish --workspaces --include-workspace-root&quot;,<br>    &quot;start&quot;: &quot;node index.js&quot;,<br>    &quot;test&quot;: &quot;npm run test --workspaces --if-present&quot;,<br>    &quot;test:module-a&quot;: &quot;npm test --workspace=./packages/module-a&quot;<br>  },<br>  &quot;//&quot;: {<br>    &quot;dev:publish&quot;: &quot;Publish all the packages on npm registry&quot;,<br>    &quot;test&quot;: &quot;Run tests for all sub-packages&quot;,<br>    &quot;test:module-a&quot;: &quot;Run tests only for @example/module-a&quot;<br>  },<br>  &quot;author&quot;: &quot;&quot;,<br>  &quot;license&quot;: &quot;ISC&quot;,<br>  &quot;description&quot;: &quot;&quot;,<br>  &quot;dependencies&quot;: {<br>    &quot;@example/module-a&quot;: &quot;1.0.0&quot;,<br>    &quot;@example/module-b&quot;: &quot;1.0.0&quot;<br>  }<br>}</pre><p>Nota Bene: on the first time, ensure you have declare the organization <strong>@example in your NPM profile</strong><a href="https://www.npmjs.com/org/create"> with this link:</a></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*yV77N2Ol95SifrOy1jdQRA.png" /><figcaption>Type to create the organization <strong>example</strong></figcaption></figure><p>Then you will find in your NPM profile the newly declared profile and see next the published packages:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*gS_4xl6s3MfBW5DFvjEjKg.png" /><figcaption>The personal organizations I have</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*YPIdcNeI6jk0lVoUmMoIEQ.png" /><figcaption>The published packages under the organization <strong>web-component-attribute-polyfill</strong></figcaption></figure><h3>Vscode integration (with Jest, Eslint, etc…)</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*nVOqDzQMDdEpL5lbk_t3nw.jpeg" /><figcaption>One IDE to rule them all</figcaption></figure><p>I usually code with <a href="https://code.visualstudio.com/">VsCode</a> for years now. And then I used the <a href="https://github.com/jest-community/vscode-jest">Jest plugin</a> and<a href="https://marketplace.visualstudio.com/items?itemName=firsttris.vscode-jest-runner"> Jest-Runner </a>plugin. For NPM workspaces, we will see we have to adjust the configurations.</p><p>First all, at the root-level, under .<strong>vscode </strong>folder, let’s an usual <strong>settings.json </strong>file:</p><pre>{<br>  &quot;jest.jestCommandLine&quot;: &quot;npx jest --no-cache&quot;,<br>  &quot;jest.nodeEnv&quot;: {<br>    &quot;NODE_OPTIONS&quot;: &quot;--experimental-vm-modules&quot;<br>  },<br>  &quot;jest.runMode&quot;: &quot;on-save&quot;,<br>  &quot;jest.debugMode&quot;: true,<br>  &quot;jest.coverageFormatter&quot;: &quot;DefaultFormatter&quot;,<br>  &quot;jest.coverageColors&quot;: {<br>    &quot;covered&quot;: &quot;rgba(9, 156, 65, 0.4)&quot;,<br>    &quot;uncovered&quot;: &quot;rgba(255,99,71, 0.2)&quot;,<br>    &quot;partially-covered&quot;: &quot;rgba(255,215,0, 0.2)&quot;<br>  },<br>  &quot;jestrunner.jestCommand&quot;: &quot;NODE_OPTIONS=\&quot;--experimental-vm-modules\&quot; npx jest&quot;,<br>  &quot;jestrunner.runOptions&quot;: [&quot;--no-cache&quot;, &quot;--coverage=false&quot;],<br>  &quot;jestrunner.debugOptions&quot;: {<br>    &quot;env&quot;: { &quot;NODE_OPTIONS&quot;: &quot;--experimental-vm-modules&quot; },<br>    &quot;args&quot;: [<br>      &quot;--runInBand&quot;,<br>      &quot;--no-cache&quot;,<br>      &quot;--coverage=false&quot;,<br>      &quot;--colors&quot;,<br>      &quot;--verbose&quot;,<br>      &quot;--watchAll=false&quot;<br>    ]<br>  }<br>}</pre><p>Within VsCode, when we run the tests with the run action or when we modify a test, the tests will be executed successfully:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*HSg4r5sliDfgrvBvTkiJXQ.png" /><figcaption>At the first glance, it seems fine</figcaption></figure><p>However for testing tab, when we will have set the tests for all packages, everything will be mixed and executed at the same time.</p><p>To organize the testing tab, we could use the <a href="https://github.com/jest-community/vscode-jest?tab=readme-ov-file#virtualfolders">jest.virtualFolders</a> to help the plugin to structure the tests:</p><pre>{<br>  &quot;jest.jestCommandLine&quot;: &quot;npx jest --no-cache&quot;,<br>  &quot;jest.nodeEnv&quot;: {<br>    &quot;NODE_OPTIONS&quot;: &quot;--experimental-vm-modules&quot;<br>  },<br>  &quot;jest.runMode&quot;: &quot;on-save&quot;,<br>  &quot;jest.debugMode&quot;: true,<br>  &quot;jest.coverageFormatter&quot;: &quot;DefaultFormatter&quot;,<br>  &quot;jest.coverageColors&quot;: {<br>    &quot;covered&quot;: &quot;rgba(9, 156, 65, 0.4)&quot;,<br>    &quot;uncovered&quot;: &quot;rgba(255,99,71, 0.2)&quot;,<br>    &quot;partially-covered&quot;: &quot;rgba(255,215,0, 0.2)&quot;<br>  },<br>  &quot;jest.virtualFolders&quot;: [<br>    { &quot;name&quot;: &quot;module-a&quot;, &quot;rootPath&quot;: &quot;packages/module-a&quot; }<br>  ],<br>  &quot;jestrunner.jestCommand&quot;: &quot;NODE_OPTIONS=\&quot;--experimental-vm-modules\&quot; npx jest&quot;,<br>  &quot;jestrunner.runOptions&quot;: [&quot;--no-cache&quot;, &quot;--coverage=false&quot;],<br>  &quot;jestrunner.debugOptions&quot;: {<br>    &quot;env&quot;: { &quot;NODE_OPTIONS&quot;: &quot;--experimental-vm-modules&quot; },<br>    &quot;args&quot;: [<br>      &quot;--runInBand&quot;,<br>      &quot;--no-cache&quot;,<br>      &quot;--coverage=false&quot;,<br>      &quot;--colors&quot;,<br>      &quot;--verbose&quot;,<br>      &quot;--watchAll=false&quot;<br>    ]<br>  }<br>}</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*gwJC11FDEKQstAFzg5Zefw.png" /><figcaption>Our tests are organized now</figcaption></figure><p>Another important aspect: at this time it executes in the same way the packages. But for some packages, we could have specificities (maybe we need more Jest plugins, a specific configuration, we don’t use the module type, etc…).</p><p>Then VsCode allows <em>in each package to create our own </em><strong><em>.vscode</em></strong><em> folder</em> and then another <strong>settings.json</strong> to <em>override only what we want to override</em>:</p><pre>{<br>  &quot;jest.jestCommandLine&quot;: &quot;npx jest --no-cache --config ./jest.config.json&quot;,<br>  &quot;jestrunner.configPath&quot;: &quot;./jest.config.json&quot;<br>}</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*BsWtLGBAhi9fv35SHXBCQQ.png" /><figcaption>Override what we want to override for VsCode’ settings</figcaption></figure><p>We will have some troubles with <a href="https://eslint.org/">Eslint</a>. The <a href="https://marketplace.visualstudio.com/items?itemName=dbaeumer.vscode-eslint">VsCode’s plugin for Eslint </a>works by default as everything is at the root level. We need then to declare on each folder level (and so packages) to check there isn’t some configuration.</p><p>It means then we could tune Eslint for each packages depending of the scope of the package (NodeJS only? Web? etc…):</p><pre>{<br>  &quot;eslint.workingDirectories&quot;: [{ &quot;mode&quot;: &quot;location&quot; }],<br>  &quot;eslint.useFlatConfig&quot;: true,<br>  &quot;jest.jestCommandLine&quot;: &quot;npx jest --no-cache&quot;,<br>  &quot;jest.nodeEnv&quot;: {<br>    &quot;NODE_OPTIONS&quot;: &quot;--experimental-vm-modules&quot;<br>  },<br>  &quot;jest.runMode&quot;: &quot;on-save&quot;,<br>  &quot;jest.debugMode&quot;: true,<br>  &quot;jest.coverageFormatter&quot;: &quot;DefaultFormatter&quot;,<br>  &quot;jest.coverageColors&quot;: {<br>    &quot;covered&quot;: &quot;rgba(9, 156, 65, 0.4)&quot;,<br>    &quot;uncovered&quot;: &quot;rgba(255,99,71, 0.2)&quot;,<br>    &quot;partially-covered&quot;: &quot;rgba(255,215,0, 0.2)&quot;<br>  },<br>  &quot;jest.virtualFolders&quot;: [<br>    { &quot;name&quot;: &quot;module-a&quot;, &quot;rootPath&quot;: &quot;packages/module-a&quot; }<br>  ],<br>  &quot;jestrunner.jestCommand&quot;: &quot;NODE_OPTIONS=\&quot;--experimental-vm-modules\&quot; npx jest&quot;,<br>  &quot;jestrunner.runOptions&quot;: [&quot;--no-cache&quot;, &quot;--coverage=false&quot;],<br>  &quot;jestrunner.debugOptions&quot;: {<br>    &quot;env&quot;: { &quot;NODE_OPTIONS&quot;: &quot;--experimental-vm-modules&quot; },<br>    &quot;args&quot;: [<br>      &quot;--runInBand&quot;,<br>      &quot;--no-cache&quot;,<br>      &quot;--coverage=false&quot;,<br>      &quot;--colors&quot;,<br>      &quot;--verbose&quot;,<br>      &quot;--watchAll=false&quot;<br>    ]<br>  }<br>}</pre><h3>Volta usage</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/840/1*TujwM0qGS5raRkCat0LIkA.jpeg" /><figcaption>Tool manager for NodeJS ecosystem</figcaption></figure><p><a href="https://volta.sh/">VoltaJs</a> is a JavaScript Tool Manager: it will download any version of what you want of NPM, NodeJs, Yarn, etc…</p><p>And then it will setup your shell to use the appropriate version, regardless where you are on your file system (e.g. if you have two projects and you setup different versions of NPM for example, when you navigate through your shell, the version of NPM will be setted automatically).</p><p>This is pretty helpfull when you have mutliple team mate on the same project or simply if someone want to fork your project, he will be able to use the appropriate set of tools.</p><p>Let’s run the following command on the root-level package:</p><pre>~/dev/github/example$ node --version &amp;&amp; npm --version<br>v22.13.1<br>10.9.2</pre><p>As you can see, the latest version NodeJs (at this time) is installed. However we want to use <strong>node@20.x</strong> instead. Let’s update our <strong>package.json</strong> like so:</p><pre>{<br>  &quot;name&quot;: &quot;example&quot;,<br>  &quot;version&quot;: &quot;1.0.0&quot;,<br>  &quot;main&quot;: &quot;index.js&quot;,<br>  &quot;type&quot;: &quot;module&quot;,<br>  &quot;private&quot;: &quot;true&quot;,<br>  &quot;workspaces&quot;: [<br>    &quot;./packages/*&quot;<br>  ],<br>  &quot;scripts&quot;: {<br>    &quot;dev:linting&quot;: &quot;npm run dev:linting --workspaces --if-present&quot;,<br>    &quot;dev:publish&quot;: &quot;npm publish --workspaces --include-workspace-root&quot;,<br>    &quot;start&quot;: &quot;node index.js&quot;,<br>    &quot;test&quot;: &quot;npm run test --workspaces --if-present&quot;,<br>    &quot;test:module-a&quot;: &quot;npm test --workspace=./packages/module-a&quot;<br>  },<br>  &quot;//&quot;: {<br>    &quot;dev:linting&quot;: &quot;Lint files over packages&quot;,<br>    &quot;dev:publish&quot;: &quot;Publish all the packages on npm registry&quot;,<br>    &quot;test&quot;: &quot;Run tests for all sub-packages&quot;,<br>    &quot;test:module-a&quot;: &quot;Run tests only for @example/module-a&quot;<br>  },<br>  &quot;author&quot;: &quot;&quot;,<br>  &quot;license&quot;: &quot;ISC&quot;,<br>  &quot;description&quot;: &quot;&quot;,<br>  &quot;dependencies&quot;: {<br>    &quot;@example/module-a&quot;: &quot;1.0.0&quot;,<br>    &quot;@example/module-b&quot;: &quot;1.0.0&quot;<br>  },<br>  &quot;devDependencies&quot;: {<br>    &quot;eslint&quot;: &quot;9.16.0&quot;,<br>    &quot;eslint-plugin-jest&quot;: &quot;28.9.0&quot;<br>  },<br>  &quot;volta&quot;: {<br>    &quot;node&quot;: &quot;20.15.0&quot;,<br>    &quot;npm&quot;: &quot;10.7.0&quot;<br>  }<br>}</pre><p>Then, if we type the above command line:</p><pre>~/dev/github/example$ node --version &amp;&amp; npm --version<br>v20.15.0<br>10.7.0</pre><p>Let’s take the opportunity to define the expected engine for our project (this is a pretty common usage to 1) inform the developers of the compatible version of your project 2) inform the consumer if he could use the project):</p><pre>{<br>  &quot;name&quot;: &quot;example&quot;,<br>  &quot;version&quot;: &quot;1.0.0&quot;,<br>  &quot;main&quot;: &quot;index.js&quot;,<br>  &quot;type&quot;: &quot;module&quot;,<br>  &quot;private&quot;: &quot;true&quot;,<br>  &quot;workspaces&quot;: [<br>    &quot;./packages/*&quot;<br>  ],<br>  &quot;scripts&quot;: {<br>    &quot;dev:linting&quot;: &quot;npm run dev:linting --workspaces --if-present&quot;,<br>    &quot;dev:publish&quot;: &quot;npm publish --workspaces --include-workspace-root&quot;,<br>    &quot;start&quot;: &quot;node index.js&quot;,<br>    &quot;test&quot;: &quot;npm run test --workspaces --if-present&quot;,<br>    &quot;test:module-a&quot;: &quot;npm test --workspace=./packages/module-a&quot;<br>  },<br>  &quot;//&quot;: {<br>    &quot;dev:linting&quot;: &quot;Lint files over packages&quot;,<br>    &quot;dev:publish&quot;: &quot;Publish all the packages on npm registry&quot;,<br>    &quot;test&quot;: &quot;Run tests for all sub-packages&quot;,<br>    &quot;test:module-a&quot;: &quot;Run tests only for @example/module-a&quot;<br>  },<br>  &quot;author&quot;: &quot;&quot;,<br>  &quot;license&quot;: &quot;ISC&quot;,<br>  &quot;description&quot;: &quot;&quot;,<br>  &quot;dependencies&quot;: {<br>    &quot;@example/module-a&quot;: &quot;1.0.0&quot;,<br>    &quot;@example/module-b&quot;: &quot;1.0.0&quot;<br>  },<br>  &quot;devDependencies&quot;: {<br>    &quot;eslint&quot;: &quot;9.16.0&quot;,<br>    &quot;eslint-plugin-jest&quot;: &quot;28.9.0&quot;<br>  },<br>  &quot;engines&quot;: {<br>    &quot;node&quot;: &quot;^16.10.0 || ^18.12.0 || &gt;=20.0.0&quot;<br>  },<br>  &quot;volta&quot;: {<br>    &quot;node&quot;: &quot;20.15.0&quot;,<br>    &quot;npm&quot;: &quot;10.7.0&quot;<br>  }<br>}</pre><p>Great.</p><p>Wait, what about the sub-packages:</p><p>If we run:</p><pre>~/dev/github/example$ cd ./packages/module-a<br>~/dev/github/example/packages/module-a$ node --version &amp;&amp; npm --version<br>v22.13.1<br>10.9.2</pre><p>We could see we use the default NodeJs version of the system.</p><p>We have two solutions:</p><ol><li>Duplicate the configuration :’(</li><li>Use the <a href="https://docs.volta.sh/advanced/workspaces">VoltaJs’ extends capability</a></li></ol><pre>{<br>  &quot;name&quot;: &quot;@example/module-a&quot;,<br>  &quot;version&quot;: &quot;1.0.0&quot;,<br>  &quot;description&quot;: &quot;The first module&quot;,<br>  &quot;main&quot;: &quot;index.js&quot;,<br>  &quot;type&quot;: &quot;module&quot;,<br>  &quot;scripts&quot;: {<br>    &quot;dev:linting&quot;: &quot;eslint --config=eslint.config.js&quot;,<br>    &quot;test&quot;: &quot;NODE_OPTIONS=\&quot;$NODE_OPTIONS --enable-source-maps --experimental-vm-modules\&quot; npx jest --no-cache --config jest.config.json&quot;,<br>    &quot;test:coverage&quot;: &quot;npm run test -- --coverage&quot;<br>  },<br>  &quot;//&quot;: {<br>    &quot;dev:linting&quot;: &quot;Lint files&quot;,<br>    &quot;test&quot;: &quot;Run Jest tests&quot;,<br>    &quot;test:coverage&quot;: &quot;Run Jest tests and collect coverage&quot;<br>  },<br>  &quot;author&quot;: &quot;&quot;,<br>  &quot;license&quot;: &quot;ISC&quot;,<br>  &quot;peerDependencies&quot;: {<br>    &quot;@example/jest-utils&quot;: &quot;1.0.0&quot;<br>  },<br>  &quot;peerDependenciesMeta&quot;: {<br>    &quot;@example/jest-utils&quot;: {<br>      &quot;optional&quot;: true<br>    }<br>  },<br>  &quot;engines&quot;: {<br>    &quot;node&quot;: &quot;^16.10.0 || ^18.12.0 || &gt;=20.0.0&quot;<br>  },<br>  &quot;volta&quot;: {<br>    &quot;extends&quot;: &quot;../../package.json&quot;<br>  }<br>}</pre><p>Then, when we type again the above command:</p><pre>~/dev/github/example$ cd ./packages/module-a<br>~/dev/github/example/packages/module-a$ node --version &amp;&amp; npm --version<br>v20.15.0<br>10.7.0</pre><p>Everything is fine! And if needed, we could adapt the version depending of our package.</p><p>The only sad aspect: <em>NPM doesn’t have</em><strong><em> package.json</em></strong><em> inheritance</em>. Meaning we have to duplicate the engines section for each sub-packages.</p><h3>Time to bump our version</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/500/1*5wuhy451cIdB8tkCSq6B_w.gif" /><figcaption>Here’s Johnny</figcaption></figure><p>Let’s go from version <strong>1.0.0</strong> to <strong>1.1.0</strong>.</p><p>We will use the <a href="https://docs.npmjs.com/cli/v11/commands/npm-version">npm version</a> command on that perspective, and we will raise at the root-level package:</p><pre>npm version minor --no-git-tag-version --allow-same-version</pre><p><em>no-git-tag-version</em> is used to prevent to create a git tag and <em>allow-same-version</em> is used if we manually provide the version and this is the same (here he should not be the case as we request to upgrade the version based on the minor’ <a href="https://semver.org/">semver semantic</a>.</p><p>If we run the command, we will have:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*49tF_6Kco1_v7MbM3UF_GQ.png" /><figcaption>Damned</figcaption></figure><p>Only the root-level’s <strong>package.json</strong> was updated. Hopefully, we have the <a href="https://docs.npmjs.com/cli/v11/commands/npm-version#workspaces">workspaces</a> and <a href="https://docs.npmjs.com/cli/v11/commands/npm-version#include-workspace-root">include-workspace-root</a> options. If we revert the changes and we run:</p><pre>npm version minor --no-git-tag-version --allow-same-version --workspaces --include-workspace-root</pre><p>All the files will be updated:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*yotK8DvvTLV-73Q5Jr1DEQ.png" /><figcaption>At least, the version of each package is updated</figcaption></figure><p>But we will have this error:</p><pre>example<br>v1.1.0<br>@example/jest-utils<br>v1.1.0<br>@example/module-a<br>v1.1.0<br>@example/module-b<br>v1.1.0<br>npm warn ERESOLVE overriding peer dependency<br>npm warn While resolving: @example/module-a@1.1.0<br>npm warn Found: @example/jest-utils@undefined<br>npm warn node_modules/@example/jest-utils<br>npm warn   workspace packages/jest-utils from the root project<br>npm warn<br>npm warn Could not resolve dependency:<br>npm warn peerOptional @example/jest-utils@&quot;1.0.0&quot; from @example/module-a@1.1.0<br>npm warn packages/module-a<br>npm warn   @example/module-a@1.0.0<br>npm warn   node_modules/@example/module-a<br>npm error code E404<br>npm error 404 Not Found - GET https://registry.npmjs.org/@example%2fjest-utils - Not found<br>npm error 404<br>npm error 404  &#39;@example/jest-utils@1.0.0&#39; is not in this registry.<br>npm error 404<br>npm error 404 Note that you can also install from a<br>npm error 404 tarball, folder, http url, or git url.<br><br>npm error A complete log of this run can be found in: /home/julien-roche/.npm/_logs/2025-02-02T12_53_05_221Z-debug-0.log</pre><p>This is logic: <strong>@example/jest-utils</strong> is a private package and we set the version to 1.0.0 in <strong>@example/module-a. </strong>As the package was updated, as as the npm version command runs a pruning command at the end, it will access everything is correct. Then it breaks the symbolic link because no <strong>@example/jest-utils@1.0.0 </strong>is present locally and then will try to download it.</p><p>That’s why we should always declare a private dependency usage with a wildcard instead:</p><pre>{<br>  &quot;name&quot;: &quot;@example/module-a&quot;,<br>  &quot;version&quot;: &quot;1.0.0&quot;,<br>  &quot;description&quot;: &quot;The first module&quot;,<br>  &quot;main&quot;: &quot;index.js&quot;,<br>  &quot;type&quot;: &quot;module&quot;,<br>  &quot;scripts&quot;: {<br>    &quot;dev:linting&quot;: &quot;eslint --config=eslint.config.js&quot;,<br>    &quot;test&quot;: &quot;NODE_OPTIONS=\&quot;$NODE_OPTIONS --enable-source-maps --experimental-vm-modules\&quot; npx jest --no-cache --config jest.config.json&quot;,<br>    &quot;test:coverage&quot;: &quot;npm run test -- --coverage&quot;<br>  },<br>  &quot;//&quot;: {<br>    &quot;dev:linting&quot;: &quot;Lint files&quot;,<br>    &quot;test&quot;: &quot;Run Jest tests&quot;,<br>    &quot;test:coverage&quot;: &quot;Run Jest tests and collect coverage&quot;<br>  },<br>  &quot;author&quot;: &quot;&quot;,<br>  &quot;license&quot;: &quot;ISC&quot;,<br>  &quot;peerDependencies&quot;: {<br>    &quot;@example/jest-utils&quot;: &quot;*&quot;<br>  },<br>  &quot;peerDependenciesMeta&quot;: {<br>    &quot;@example/jest-utils&quot;: {<br>      &quot;optional&quot;: true<br>    }<br>  },<br>  &quot;engines&quot;: {<br>    &quot;node&quot;: &quot;^16.10.0 || ^18.12.0 || &gt;=20.0.0&quot;<br>  },<br>  &quot;volta&quot;: {<br>    &quot;extends&quot;: &quot;../../package.json&quot;<br>  }<br>}</pre><p>Run a pruning command, create a git commit.</p><p>If we rerun the above npm version command, we will have then:</p><pre>~/dev/github/example$ npm version minor --no-git-tag-version --allow-same-version --workspaces --include-workspace-root<br>example<br>v1.1.0<br>@example/jest-utils<br>v1.1.0<br>@example/module-a<br>v1.1.0<br>@example/module-b<br>v1.1.0<br><br>up to date in 1s<br><br>57 packages are looking for funding<br>  run `npm fund` for details</pre><p>At the first glance, it seems fine.</p><p>But if we look closely, we will see the inter-dependencies are not updated. and we still use the <strong>1.0.0 </strong>version:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/958/1*2sVqwg-SYo_ZxMHGsn5DxQ.png" /><figcaption>The version used at the root-level is not updated. This will happen also for sub-packages with inter-dependencies.</figcaption></figure><p>It seems to be intended and <em>we have to manually change the versions and run the pruning command</em>.</p><p>This is a debate.</p><p>In my point of view, we use a mono-repo. And the npm version command enforces to create a git tag (this is enabled by default). And as we have a mono-repo, we couldn’t update independently the packages of the workspaces and then have a git tag for each packages… except if we don’t have a mono-repo -_-’.</p><p>In this example, we will do it manually.</p><p>But you could create a script to automate it if you want.</p><p>I recommend the usage of <a href="https://www.npmjs.com/package/@npmcli/package-json">@npmcli/package-json</a> module.</p><p>This is a module used within NPM and it updates the <strong>package.json </strong>files in the same way as NPM commands (this is logic as this used by default).</p><p>Why no usage of npm install commands? Because we will face to a looooooooot of troubles &gt;_&lt;.</p><p>If we run the following after the global bumping (if we used module-b as a dependency of module-a):</p><pre>npm install --save @example-b@1.1.0 -w ./packages/module-a</pre><p>Sometimes, it will work. Sometimes not…</p><p>So we will use the force option:</p><pre>npm install --save @example-b@1.1.0 -w ./packages/module-a --force</pre><p>Sometimes, it will work. Sometimes not… Again!!</p><p>We will have some weird error:</p><pre>npm ERR! Cannot set properties of null (setting &#39;dev&#39;)</pre><p><a href="https://github.com/npm/cli/issues/4064">And this error seems to be known… and sometimes it appears, sometimes it disappears…</a></p><p>IMO, I recommend to modify the <strong>package.json</strong> files manually or with a script. For a tool I made, I create a package for this perspective: <a href="https://github.com/rochejul/npmversion/tree/master/packages/workspace">@npmversion/workspace</a>.</p><h3>Conclusion</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*VVOBJ4nWh2Q3S_OvjfKDnA.jpeg" /><figcaption>What such an experience</figcaption></figure><p>This is really really to see we could organize our packages within a mono-repo.</p><p>With some basics commands, we could create the hierarchy, create inter-dependencies, quickly. In my sense, the only trick is about the private packages.</p><p>Some configuration is required for our IDE, and also for some tools but no real blockers.</p><p>In my sense, the main troubles came with the bumping part. And it was painful to find a safe path.</p><p>Of course, some nice features could be possible, like package.json inheritance (I know <a href="https://classic.yarnpkg.com/en/package/json-inherit">Yarn allows this feature)</a>. But I prefer to use the Vanilla approach and then embrace the tools capabilities of NodeJs.</p><p>But now I’m happy to use NPM workspaces.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e349da921d29" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>