Surface

The box every effect of Axiom draws on: it owns the shape, the tilt and the frame scheduler, and it paints its content with layers.

A surface is the box that every visual effect of Axiom Design System draws on. Wrap your element with ax-surface, pick the layers you need in the layers attribute, and give it a shape: your content always stays on top of the layers.

The geometry (corners, side features, the border ring and what a tilt does to it) is the Shape language, documented on the Shape page at /components/shape. This page is about the element itself: the layers, the tilt, the clip and the one frame scheduler that every surface on the page shares.

Surfaces only work while they are visible, read their styles only after something changed, and lower the frame rate of far and small surfaces when the page starts dropping frames. Shape properties are not inherited, so set them on the ax-surface element itself. The Playground (/playground) has every property with live controls.

Installation

shell
npm install @ax-design/surface

and import it:

js
import { register } from '@ax-design/surface';
register();
Layers

Layers

The layers attribute lists what the surface draws, in order. Your content is on top of all of them. Fill comes with Surface; the other layers come from their own packages: elevation (a shadow that follows the shape), reveal and acrylic.

Put elevation first, so the other layers paint on top of it. Pick a list below. The background of this preview is an image, so the acrylic has something to blur.

Try it

16
js
import { register } from '@ax-design/surface';
import { registerAcrylicLayer } from '@ax-design/acrylic';
import { registerElevationLayer } from '@ax-design/elevation';
import { registerRevealLayer } from '@ax-design/reveal-highlight';

registerAcrylicLayer();
registerElevationLayer();
registerRevealLayer();
register();
html
<ax-surface class="shape" layers="acrylic reveal">...</ax-surface>
css
.shape {
  --elevation-depth: 16;
}
Card

Fill and border

The fill layer paints the inside of the shape with --ax-fill-color and its edge with --ax-border-color. The edge is the border ring the Shape page describes: an outer and an inner contour, filled with the even-odd rule.

The border is as wide on every side as --ax-border-width says, or per side with --ax-border-top-width and its siblings. While the surface tilts, the border keeps the same thickness on screen; that work is described with the tilt on the Shape page.

Try it

#FFFFFF
14%
2
2
html
<ax-surface class="card" layers="fill">...</ax-surface>
css
.card {
  --ax-fill-color: rgba(255, 255, 255, 0.14);
  --ax-border-color: white;
  --ax-border-width: 2px;
  --ax-border-bottom-width: 2px;
}
Press me

Tilt

Set --ax-tilt-mode and the surface tilts toward the point you press. Absolute sinks the pressed side by --ax-tilt-depth, relative tilts it by --ax-tilt-factor degrees. --ax-tilt-zoom drops the whole surface when you press its center, and --ax-tilt-perspective gives the effect its depth. Press the shape to try it.

The tilt properties inherit, so set them once on a container and every surface inside tilts the same way. --ax-tilt-direction: inverted lifts the pressed side instead of sinking it.

Try it

3
html
<ax-surface class="shape" layers="fill">...</ax-surface>
css
.shape {
  --ax-tilt-mode: absolute;
  --ax-tilt-depth: 3px;    /* absolute */
  --ax-tilt-factor: 8;         /* relative, in degrees */
  --ax-tilt-direction: normal;
}

Clip

Add the clip attribute and the content of the surface is cut to its shape, so an image or a video fills a tile of any shape. The cut follows the shape when it changes, and tilts with the content when the surface is pressed.

Without clip the content keeps its own box, and the shape only draws around it.

Try it

24 %
html
<ax-surface class="tile" clip layers="reveal">
  <img src="cover.jpg" alt="" style="display: block; width: 100%; height: 100%; object-fit: cover">
</ax-surface>
css
.tile {
  width: 220px;
  height: 220px;
  --ax-radius: 24%;
  --ax-corner-shape: scoop;
}
Press

Surfaces inside surfaces

A surface inside a tilted surface follows the tilt above it: the pointer is mapped through every tilt on the way down, so the inner surface still tilts toward the point you press, and a reveal highlight stays under your pointer. Press the inner shape and watch both lean.

This is how the components compose: ax-reveal inside ax-pressure, a button inside a card. Each surface keeps its own shape and its own layers, and they still share the one scheduler.

Try it

4
html
<ax-surface class="card" layers="fill">
  <ax-surface class="button" layers="fill reveal">
    <button>Press</button>
  </ax-surface>
</ax-surface>
css
.card {
  --ax-radius: 26px;
  --ax-tilt-mode: absolute;
  --ax-tilt-depth: 4px;
}

.button {
  position: absolute;
  inset: 0;
  margin: auto; /* centers the inner surface in the card */
  --ax-radius: 14px;
  --ax-tilt-mode: absolute;
  --ax-tilt-depth: 3px;
}
Turn me

Motion

motion() turns the surface in 3D and fades it. It lasts one frame: call it on every frame of an animation, and with null to end it. The preview gives the surface a new state on every frame, from the two sliders: rotateY turns it about its vertical axis, opacity fades it.

The layers follow the motion: the reveal light turns with the surface, and the fade is set on the layer itself, never on an ancestor, so the backdrop of acrylic and reveal stays right.

Try it

30
1
js
const surface = document.querySelector('ax-surface');

// Every frame, with the state of that frame.
const frame = () => {
  surface.motion({ rotateY: 30, opacity: 1 });
  requestAnimationFrame(frame);
};
requestAnimationFrame(frame);

// Later: surface.motion(null) ends the motion.
Press

Press from code

press() presses the surface as a pointer press does, for a keyboard activation: the tilt leans and the layers see a pressed pointer. release() ends the press. Use them to show a press from code, such as a button that a keyboard user has activated.

pressRemaining() tells how many milliseconds the press feedback still plays, so that an action can wait for it, as a tile does before it runs its action.

html
<ax-surface class="shape" layers="fill reveal">...</ax-surface>
js
const surface = document.querySelector('.shape');

surface.press();      // the tilt leans, the layers see a pressed pointer
surface.release();    // the press ends

// Or let an action wait for the press feedback:
surface.press();
requestAnimationFrame(() => setTimeout(activate, surface.pressRemaining()));
The preview in forced colors with a dark theme.The preview in forced colors with a light theme.

Accessibility

Keyboard: Add the focus layer and the surface draws the focus ring of Windows around its shape while an element in it has keyboard focus. Press Tab to reach the button in the preview. Its own browser outline is off, so the ring is the only focus mark.

Screen readers: Surface sets no role and no name, so the content gives them: the button has its own text. The drawings of the layers are hidden from assistive technology.

High contrast (forced colors): The fill turns into Canvas, the border into CanvasText, and the focus rings into CanvasText and Canvas. The browser does not recolor SVG or canvas drawings, so the layers do it themselves.

Reduced motion: The tilt takes its pose at once instead of easing into it. A pressed surface still tilts.

Reduced transparency: Effects with a solid fallback, such as acrylic and reveal, use it. Choose Fallback in the switch to see it live.

More contrast: Surfaces do not change by themselves. The code raises the border and the focus ring for users who ask for more contrast.

Try it: in Chrome or Edge, open DevTools, then Rendering, and emulate the CSS media features prefers-reduced-motion, forced-colors or prefers-reduced-transparency. Or turn on the setting of your system: Show animations, Contrast themes and Transparency effects on Windows; Reduce motion, Increase contrast and Reduce transparency on macOS.

Try it

css
@media (prefers-contrast: more) {
  ax-surface {
    --ax-border-color: rgb(0, 0, 0);
    --ax-focus-ring-color: rgb(0, 0, 0);
    --ax-focus-ring-secondary-color: rgb(255, 255, 255);
  }
}

Reference

layers attribute

space separated names

The layers the surface draws, in order. fill comes with Surface; elevation, reveal, acrylic and your own registered layers add to it.

clip attribute

boolean, default off

Cuts the content to the shape. The cut follows the shape and the tilt.

observe attribute

precise

Use it when a style of yours depends on something a surface cannot see, such as an element inside another component's shadow root. Then call refresh() after the change, or let the precise detector watch every style of the page.

--ax-radius, --ax-corner-*, --ax-edge-* property

as on the Shape page

The shape of the surface. Every corner, corner shape, shoulder, shift and side feature is documented on the Shape page (/components/shape).

--ax-border-width property

<length>, default 1px

The width of the border on every side. It stays equally thick on screen while the surface tilts.

--ax-border-{side}-width property

<length>, default -1px

The width of the border on one side (top, right, bottom, left). A negative value means use --ax-border-width.

--ax-tilt-mode property

off | absolute | relative, default off

Tilts the surface toward the point you press. Inherited.

--ax-tilt-depth property

<length>, default 1px

For absolute mode: how far the pressed side sinks.

--ax-tilt-factor property

<number>, default 2

For relative mode: the angle in degrees.

--ax-tilt-zoom property

<length>, default 0.8px

How far the whole surface falls when you press its center.

--ax-tilt-perspective property

<length>, default 25px

The perspective of the tilt.

--ax-tilt-direction property

normal | inverted, default normal

Inverted lifts the pressed side instead of sinking it.

--ax-fill-color property

<color>, default transparent

For the fill layer: the color inside the shape.

--ax-border-color property

<color>, default transparent

For the fill layer: the color of the border.

refresh() method

() => void

Reads the styles of this surface again. Use it after a change the surface cannot notice.

refreshAll() method

() => void

Reads the styles of every surface again. Exported by the package.

release() method

() => void

Ends a press before the pointer is up, for containers that take the gesture over (a tile drag). Since 0.2.1.

registerLayer(name, create) method

(name: string, create: () => SurfaceLayer) => void

Registers a layer you wrote, to use in the layers attribute. The name must be new on the page.

hasLayer(name) method

(name: string) => boolean

True when a layer with that name is registered. register() uses it, so two packages may both claim fill.

SurfaceLayer property

{ schema?, static?, tracksPointer?, tiltsItself?, reach?, overflow?, attach, detach, write }

What a layer is. attach gets a container to draw in; write(frame) draws one frame and returns true to ask for another; overflow tells the surface how far outside the shape the layer paints.

SurfaceFrame property

{ width, height, paint, shape, borderWidths, uniformBorder, borderInner, ringInner(), tilt, tiltTransform, pointer, values, changed, quality, now }

What write receives: the resolved shape, the border ring (as an exact stroke when uniformBorder is set, otherwise as inner points), the current tilt, the pointer and the properties, plus a quality hint from the scheduler.

AxSurface method

class

The element class. Subclass it to make your own element with its own property names: layerNames_() picks the layers, shapeFor() and borderWidthsFor() read the shape, tiltParams() and tiltPerspective() read the tilt, and a unique static schemaKey keeps its schema apart.

SHAPE_SCHEMA, BORDER_SCHEMA, TILT_SCHEMA property

PropertySpec[]

The three groups of the property contract, exported from the @ax-design/surface/schema subpath: shape, border and tilt. They carry the names, syntax and initial values without the element, for a tool that only needs the contract.

SHAPE, BORDER, TILT property

{ read }

The groups of the property contract, each with the properties to read. A per-side or per-corner property left at its auto initial is resolved to the shared value in JavaScript, so a consumer needs no CSS fallback to reach each side or corner.

focus attribute

layer name

A layer in the layers attribute: a focus ring that follows the shape, for keyboard focus only (:focus-visible; :-moz-focusring before Firefox 85). The rings are drawn outside the element, so an ancestor with overflow: hidden cuts them.

--ax-focus-ring-width, --ax-focus-ring-secondary-width property

<length>, default 2px, 1px

For the focus layer: the width of the outer (primary) ring and of the inner (secondary) ring.

--ax-focus-ring-offset property

<length>, default 0px

For the focus layer: the gap between the shape and the inner ring.

--ax-focus-ring-color, --ax-focus-ring-secondary-color property

<color>, default black, rgba(255, 255, 255, 0.6)

For the focus layer: the colors of the outer and the inner ring. In forced colors mode they are CanvasText and Canvas.

motion(state) method

(state: { rotateX?, rotateY?, translateZ?, perspective?, originX?, originY?, scale?, x?, y?, opacity? } | null) => void

Moves the surface in 3D and fades it, for one frame. rotateX and rotateY (degrees), translateZ (px) and perspective (px, default 1000) turn it about originX and originY (local px, default its center). scale (default 1), x and y (px) enlarge and move it outside the perspective. opacity (0 to 1) fades it. Call it on every frame; null ends the motion.

press(point?) method

(point?: 'center' | { x: number; y: number }) => void

Presses the surface as a pointer press does, for a keyboard activation or from code. Without an argument, or with center, it presses the center of the layout box; with { x, y }, that point in local px. A pointer press in progress wins over it.

pressTakenOver property

boolean

True after release() ended a pointer press early, until the next press starts. It lets the click after a drag be swallowed.

pressRemaining() method

() => number

The milliseconds until the press feedback has finished: the longest of the layers and the tilt. 0 with reduced motion. Read it after a frame has drawn the press.

appearance() method

() => { forcedColors, reducedTransparency, reducedMotion, alwaysUseFallback }

The display preferences of the user, as the layers see them.

setAlwaysUseFallback(on) method

(on: boolean) => void

Makes the effects with a solid fallback (acrylic, reveal, elevation) use it, as Windows does on battery saver or slow hardware.

windowsEasing(id) method

(id: number) => string

The timing function of the Windows 8 and 8.1 animation theme as CSS. windowsEasing(54) is the Start screen coming in.

WINDOWS_EASINGS property

table

All twenty curves of the Windows animation theme, by number.

parseEasing(value) method

(value: string) => curve

Reads a CSS timing function (a keyword or cubic-bezier()) for a script animation with requestAnimationFrame.

bezierAt(curve, t) method

(curve, t: number) => number

The progress of a curve from parseEasing() at t, from 0 to 1.