Device

Built-in device detection and responsive scaling in Design system 1.

Design system 1 includes automatic mobile and device detection with integrated responsive scaling. The device module automatically detects device type, sets CSS classes, and configures the scaling factor.

When Design system 1 loads, it automatically:

  1. Detects whether the device is mobile, tablet, or desktop
  2. Adds .mobile or .desktop class to the <html> element
  3. Sets the --ds-sf CSS custom property for scaling
  4. Listens for resize and orientation changes

Returns true if the current device is mobile (phone or tablet).

<code><span class="line"><span style="color:var(--docs-code-keyword)">import</span><span style="color:var(--docs-code-ink)"> { detectMobileDevice } </span><span style="color:var(--docs-code-keyword)">from</span><span style="color:var(--docs-code-string)"> "design-system-1"</span><span style="color:var(--docs-code-ink)">;</span></span><span class="line"></span><span class="line"><span style="color:var(--docs-code-keyword)">if</span><span style="color:var(--docs-code-ink)"> (</span><span style="color:var(--docs-code-tag)">detectMobileDevice</span><span style="color:var(--docs-code-ink)">()) {</span></span><span class="line"><span style="color:var(--docs-code-ink)">  console.</span><span style="color:var(--docs-code-tag)">log</span><span style="color:var(--docs-code-ink)">(</span><span style="color:var(--docs-code-string)">"Mobile device"</span><span style="color:var(--docs-code-ink)">);</span></span><span class="line"><span style="color:var(--docs-code-ink)">} </span><span style="color:var(--docs-code-keyword)">else</span><span style="color:var(--docs-code-ink)"> {</span></span><span class="line"><span style="color:var(--docs-code-ink)">  console.</span><span style="color:var(--docs-code-tag)">log</span><span style="color:var(--docs-code-ink)">(</span><span style="color:var(--docs-code-string)">"Desktop device"</span><span style="color:var(--docs-code-ink)">);</span></span><span class="line"><span style="color:var(--docs-code-ink)">}</span></span></code>

Detection combines:

  • User agent string matching
  • Touch capability (maxTouchPoints > 1)
  • Viewport size (narrow viewport ≤ 820px)

Returns detailed device information.

<code><span class="line"><span style="color:var(--docs-code-keyword)">import</span><span style="color:var(--docs-code-ink)"> { getDeviceInfo } </span><span style="color:var(--docs-code-keyword)">from</span><span style="color:var(--docs-code-string)"> "design-system-1"</span><span style="color:var(--docs-code-ink)">;</span></span><span class="line"></span><span class="line"><span style="color:var(--docs-code-keyword)">const</span><span style="color:var(--docs-code-ink)"> info </span><span style="color:var(--docs-code-keyword)">=</span><span style="color:var(--docs-code-tag)"> getDeviceInfo</span><span style="color:var(--docs-code-ink)">();</span></span><span class="line"><span style="color:var(--docs-code-comment)">// {</span></span><span class="line"><span style="color:var(--docs-code-comment)">//   isMobile: boolean,</span></span><span class="line"><span style="color:var(--docs-code-comment)">//   isTablet: boolean,</span></span><span class="line"><span style="color:var(--docs-code-comment)">//   isDesktop: boolean,</span></span><span class="line"><span style="color:var(--docs-code-comment)">//   isTouchCapable: boolean,</span></span><span class="line"><span style="color:var(--docs-code-comment)">//   deviceType: 'mobile' | 'tablet' | 'desktop',</span></span><span class="line"><span style="color:var(--docs-code-comment)">//   userAgent: string,</span></span><span class="line"><span style="color:var(--docs-code-comment)">//   screenWidth: number,</span></span><span class="line"><span style="color:var(--docs-code-comment)">//   screenHeight: number</span></span><span class="line"><span style="color:var(--docs-code-comment)">// }</span></span></code>

Manually trigger device detection and scaling update.

<code><span class="line"><span style="color:var(--docs-code-keyword)">import</span><span style="color:var(--docs-code-ink)"> { initDeviceDetection } </span><span style="color:var(--docs-code-keyword)">from</span><span style="color:var(--docs-code-string)"> "design-system-1"</span><span style="color:var(--docs-code-ink)">;</span></span><span class="line"></span><span class="line"><span style="color:var(--docs-code-keyword)">const</span><span style="color:var(--docs-code-ink)"> deviceInfo </span><span style="color:var(--docs-code-keyword)">=</span><span style="color:var(--docs-code-tag)"> initDeviceDetection</span><span style="color:var(--docs-code-ink)">();</span></span></code>

This is called automatically on load and resize, but can be invoked manually if needed.

The <html> element receives device-specific classes:

<code><span class="line"><span style="color:var(--docs-code-comment)">/* Target mobile devices */</span></span><span class="line"><span style="color:var(--docs-code-tag)">html</span><span style="color:var(--docs-code-keyword)">.mobile</span><span style="color:var(--docs-code-keyword)"> .my-element</span><span style="color:var(--docs-code-ink)"> {</span></span><span class="line"><span style="color:var(--docs-code-keyword)">  font-size</span><span style="color:var(--docs-code-ink)">: </span><span style="color:var(--docs-code-number)">18</span><span style="color:var(--docs-code-keyword)">px</span><span style="color:var(--docs-code-ink)">;</span></span><span class="line"><span style="color:var(--docs-code-ink)">}</span></span><span class="line"></span><span class="line"><span style="color:var(--docs-code-comment)">/* Target desktop devices */</span></span><span class="line"><span style="color:var(--docs-code-tag)">html</span><span style="color:var(--docs-code-keyword)">.desktop</span><span style="color:var(--docs-code-keyword)"> .my-element</span><span style="color:var(--docs-code-ink)"> {</span></span><span class="line"><span style="color:var(--docs-code-keyword)">  font-size</span><span style="color:var(--docs-code-ink)">: </span><span style="color:var(--docs-code-number)">14</span><span style="color:var(--docs-code-keyword)">px</span><span style="color:var(--docs-code-ink)">;</span></span><span class="line"><span style="color:var(--docs-code-ink)">}</span></span></code>

Disable browser zoom gestures for a native app feel.

<code><span class="line"><span style="color:var(--docs-code-keyword)">import</span><span style="color:var(--docs-code-ink)"> { applike } </span><span style="color:var(--docs-code-keyword)">from</span><span style="color:var(--docs-code-string)"> "design-system-1"</span><span style="color:var(--docs-code-ink)">;</span></span><span class="line"></span><span class="line"><span style="color:var(--docs-code-tag)">applike</span><span style="color:var(--docs-code-ink)">();</span></span></code>

This prevents:

  • Double-tap to zoom
  • Pinch-to-zoom
  • Ctrl/Cmd + scroll wheel zoom

A page travels the way it is going, and never twice.

On a phone or a tablet a page change inside one document slides the whole page in one view transition: onward in from the right, back out to the right, 240ms on an ease-out curve in transform alone. A computer keeps the browser’s plain crossfade, however narrow its window, and reduced motion swaps the page in place.

A swipe from the screen’s edge is the browser’s own animation: Safari slides the page arriving in under the finger. The page never plays a second animation over it. A transition started after the swipe captures the page just left and slides or fades it over the one the swipe has already shown, a flash of where the person came from, which the browser’s own Back and Forward buttons never cause.

  • Inside one document, a popstate whose hasUAVisualTransition is true draws the route plainly: no startViewTransition, neither the slide nor the crossfade, and a transition still running is retired with skipTransition() first, so its old snapshot cannot flash either. A route that prepares its data before it is drawn draws plainly too, once prepared.
  • Between documents no event reports the swipe (whatwg/html#10831), so the page notes a touch that begins within a square of either edge. A pageswap within 1.5 seconds of it, on a traverse, skips its transition and leaves the note in session storage; the document arriving skips its pagereveal transition on the note and spends it at once (15 seconds at most). A tap at the edge that follows a link is a push and still slides.

Back and Forward in the browser’s own controls animate nothing themselves, so they keep the page’s slide.

Device detection and scaling are unified: this module puts the scale on the page with the scaling module’s rule, setting --ds-sf and data-mobile on the root and emitting scaling-changed. A computer uses --ds-sf: 1, however narrow its window, unless the page asks for Desktop aligned, while phones and tablets calculate a scaling factor based on the page’s width. A page that needs only the scale calls startScaling() from the scaling module instead. See Scaling for details.

  1. Test on real devices when possible
  2. Use the --ds-sf CSS variable for responsive sizing
  3. Design mobile-first
  4. Ensure touch targets are at least 44×44px
  5. Test both portrait and landscape orientations