GlassGroup
Two ways to keep a page full of glass cheap while it scrolls. A bake draws glass whose backdrop moves with it once, at rest, and scrolls it for free; a GlassGroup lets plain glass share filters while the page scrolls.
import { BakeProvider, GlassGroup, OutOfGlassGroup, useBakeRoot } from "@adamexu/glass";Every piece of glass is a backdrop filter, and Chrome runs each one again on every frame the page moves, whatever its size. A page with a few pieces of glass never notices. A page with dozens (a grid of buttons, a list of rows with glass in them) drops frames while it scrolls. Both tools here draw the same pixels as glass on its own; they only change what it costs.
Bake
Glass whose backdrop cannot change under it, because everything it reads scrolls along with it, is drawn once at rest into the page's own tiles, and costs nothing while the page scrolls. useBakeRoot makes a root for what is inside an element; hand it to the glass through BakeProvider. Anything that could change the backdrop puts the glass near it back on its live filters until things have been still for a moment: a press, a change to the page, an animation.
- Without
rigid, the root must sit on a flat colour: the nearest opaque background above it, with nothing else painted in between (a list on its grouped background). - With
rigid, the backdrop is inside the root too and scrolls in one piece with its glass, so it can be anything. Give the root its own background.
GlassGroup
Plain glass inside a group shares filters while the page scrolls: one stack of filters per band instead of one per glass. Nothing merges, every glass keeps its own material, and each goes back to its own filters once scrolling stops, so a press only redraws itself. Glass that is baked is not batched: a group is for where a bake is off or cannot apply.
These stay out of a group and draw their own: glass with a fixed margin or scales, standalone glass, and regular glass whose shorter side is 80 or more (it casts a shadow, which must lie under its own glass).
Together
A page of buttons that scrolls with its own backdrop: a rigid bake root, with a group around the glass. Glass buttons stand 100 apart, so a press on one does not redraw its neighbours: each filter reaches 40 past its glass. Scroll the page and press the buttons.
const root = useRef(null);
const bake = useBakeRoot(root, { rigid: true });
<BakeProvider value={bake}>
<div ref={root} style={{ position: "relative", isolation: "isolate", background: "var(--surface)" }}>
<Artwork />
<GlassGroup style={{ display: "grid", gridTemplateColumns: "repeat(3, max-content)", gap: 100 }}>
<Button buttonStyle="glass">Button</Button>
<Button buttonStyle="glass" icon={Heart}>Favorite</Button>
<Button buttonStyle="glass" icon={Plus} borderShape="circle" labelStyle="iconOnly">Add</Button>
<Button buttonStyle="glass" icon={Share} labelStyle="iconOnly">Share</Button>
<Button buttonStyle="glassProminent" tint="#ff375f">Continue</Button>
</GlassGroup>
</div>
</BakeProvider>OutOfGlassGroup
A group's shared filters are drawn at the back of the group, so anything opaque between the group and its glass would hide them while the page scrolls. Glass inside OutOfGlassGroup draws its own layers: wrap a part of the group with opaque content in it, such as a list's rows. For one piece of glass, standalone on the GlassEffect does the same.
API
useBakeRoot(root, options?)
| Prop | Type | Default | Description |
|---|---|---|---|
| root | RefObject<HTMLElement | null> | - | The element whose glass is baked: everything the glass reads must be inside it, or the flat colour under it. |
| options.rigid | boolean | false | The backdrop is inside the root and scrolls with it, so it need not be a flat colour. |
| returns | BakeApi | null | - | The value for BakeProvider. null on the server, on the first render and with ?nobake. |
BakeProvider
| Prop | Type | Default | Description |
|---|---|---|---|
| value | BakeApi | null | - | What useBakeRoot returned. null: the glass inside is not baked. |
| children | ReactNode | - | The glass to bake, inside the root. |
GlassGroup
| Prop | Type | Default | Description |
|---|---|---|---|
| className | string | - | On the group element. |
| style | CSSProperties | - | On the group element, for layout. The group is position: relative and isolation: isolate unless this says otherwise. |
| children | ReactNode | - | The glass that shares filters. |
OutOfGlassGroup
| Prop | Type | Default | Description |
|---|---|---|---|
| children | ReactNode | - | Glass that draws its own layers inside a group. |