← All items

Layer select

primitive · T1 · v1.0.0 · ready

About

CSS picks the native layer with `@supports` and falls back to the static state on its own. This primitive tells JavaScript whether to mount the L2 driver, and honours the forced override set by the showcase (`?driver=native|js|static`).

Layers

Static
n/a: no visual output.

Accessibility

Selects the static layer under prefers-reduced-motion: reduce (unless overridden).

Sources

  • original

Code

Download zip

Self-contained: copy every file, keeping the folder structure.

layer.ts
// web-effects layer-select 1.0.0 · MIT-0
// Browser side of layer selection: reads the current inputs and notifies on change.
// Framework-free, for plain-browser hosts; use-layer.ts wraps it for React.
import {
  layerAttribute,
  parseLayer,
  selectLayer,
  type Layer,
} from "./select-layer";

const reducedMotionQuery = "(prefers-reduced-motion: reduce)";

/**
 * The layer for an item whose native layer needs `supports` (an `@supports` condition, the same
 * text as in the item's CSS), or null when the item has no native layer.
 */
export function currentLayer(
  supports: string | null,
  prerequisitesMet = true,
): Layer {
  return selectLayer({
    nativeSupported: supports !== null && CSS.supports(supports),
    reducedMotion: matchMedia(reducedMotionQuery).matches,
    prerequisitesMet,
    override: parseLayer(document.documentElement.getAttribute(layerAttribute)),
  });
}

/** Calls `onChange` when reduced motion or the forced override changes. Returns a stop function. */
export function watchLayerInputs(onChange: () => void): () => void {
  const media = matchMedia(reducedMotionQuery);
  media.addEventListener("change", onChange);
  const observer = new MutationObserver(onChange);
  observer.observe(document.documentElement, {
    attributes: true,
    attributeFilter: [layerAttribute],
  });
  return () => {
    media.removeEventListener("change", onChange);
    observer.disconnect();
  };
}
select-layer.ts
// web-effects layer-select 1.0.0 · MIT-0
// Layer selection (D2.4, D3.6). CSS decides on its own: `@supports` enables L1, and the base
// rules are the L3 static state, so server-rendered HTML works without JS. JavaScript only
// decides whether to mount an L2 driver; this is that decision, as a pure function.
//
// Forced override (R6.1): an attribute on <html>. Item CSS honours it by writing native-layer
// and reduced-motion rules as `:root:not([data-fx-layer]) .x, :root[data-fx-layer="native"] .x`.
// The showcase sets the attribute from `?driver=`; items never read the host's URL.

export const layers = ["native", "js", "static"] as const;
export type Layer = (typeof layers)[number];

export const layerAttribute = "data-fx-layer";

export function parseLayer(value: string | null | undefined): Layer | null {
  return layers.find((layer) => layer === value) ?? null;
}

export type LayerInputs = {
  /** The item's `@supports` condition holds; false for items without a native layer. */
  nativeSupported: boolean;
  reducedMotion: boolean;
  /** Inputs the effect cannot work without (e.g. a fine pointer) are present. */
  prerequisitesMet: boolean;
  /** Forced layer from the `data-fx-layer` attribute; wins over everything else. */
  override: Layer | null;
};

export function selectLayer({
  nativeSupported,
  reducedMotion,
  prerequisitesMet,
  override,
}: LayerInputs): Layer {
  if (override) return override;
  if (reducedMotion || !prerequisitesMet) return "static";
  return nativeSupported ? "native" : "js";
}
use-layer.ts
// web-effects layer-select 1.0.0 · MIT-0
import { useCallback, useSyncExternalStore } from "react";
import { currentLayer, watchLayerInputs } from "./layer";
import type { Layer } from "./select-layer";

/**
 * The active layer, or null during server rendering and hydration (CSS alone decides until then).
 * Mount the L2 driver when this returns "js".
 */
export function useLayer(
  supports: string | null,
  prerequisitesMet = true,
): Layer | null {
  const getSnapshot = useCallback(
    () => currentLayer(supports, prerequisitesMet),
    [supports, prerequisitesMet],
  );
  return useSyncExternalStore(watchLayerInputs, getSnapshot, () => null);
}