GlassEffectContainer
Glass inside a container is drawn as one piece: members merge where they come within spacing of each other, and morph when they move, change shape, appear or leave.
import { GlassEffect, GlassEffectContainer } from "@adamexu/glass";Merge and morph
Every GlassEffect inside a container is a member: the container draws their glass, merged. Close together the circles melt into one shape; spread apart they pull away from each other along the container's animation.
const [apart, setApart] = useState(false);
const step = apart ? 80 : 60;
<GlassEffectContainer spacing={20} animation="bouncy" style={{ position: "relative", width: 216, height: 56 }}>
{icons.map(({ id, Icon }, i) => (
<GlassEffect key={id} id={id} shape={{ type: "circle" }} glass={{ interactive: true }} width={56} height={56}
style={{ position: "absolute", top: 0, left: 80 + (i - 1) * step }}>
<Icon size={24} strokeWidth={2} />
</GlassEffect>
))}
</GlassEffectContainer>
<Button buttonStyle="glass" onClick={() => setApart(!apart)}>{apart ? "Bring together" : "Spread apart"}</Button>An id matches a member across changes. Tools that appear with ids of their own grow out of the button next to them and shrink back into it. Tap the plus.
const [expanded, setExpanded] = useState(false);
const [tool, setTool] = useState("pencil");
<GlassEffectContainer spacing={40} style={{ display: "flex", alignItems: "center", gap: 12, height: 120 }}>
{expanded && tools.map(({ id, label, Icon }) => (
<button key={id} type="button" aria-label={label} onClick={() => setTool(id)}>
<GlassEffect id={id} shape={{ type: "circle" }} width={56} height={56}
glass={{ interactive: true, tint: tool === id ? "#0a84ff" : undefined }}>
<Icon size={24} strokeWidth={2} color={tool === id ? "#fff" : undefined} />
</GlassEffect>
</button>
))}
<button type="button" aria-label={expanded ? "Collapse" : "Expand"} onClick={() => setExpanded(!expanded)}>
<GlassEffect id="toggle" shape={{ type: "circle" }} glass={{ interactive: true }} width={64} height={64}>
<Plus size={24} strokeWidth={2} style={{ transform: `rotate(${expanded ? 45 : 0}deg)` }} />
</GlassEffect>
</button>
</GlassEffectContainer>Union
Members with the same union are drawn as one shape over all their frames, however far apart.
const unionOf = (mode, index) =>
mode === "none" ? undefined : mode === "all" ? "all" : mode === "halves" ? `h${index < 3 ? 0 : 1}` : `p${index >> 1}`;
<GlassEffectContainer spacing={8} style={{ display: "flex", flexWrap: "wrap", gap: 30, justifyContent: "center" }}>
{items.map(({ id, Icon }, index) => (
<GlassEffect key={id} id={id} union={unionOf(mode, index)} width={58} height={58}>
<Icon size={24} strokeWidth={2} />
</GlassEffect>
))}
</GlassEffectContainer>Transitions
transition sets how a member appears and leaves. matchedGeometry (also what happens when it is left out) grows it out of the nearest member within spacing and back into it, materialize fades it in and out, and identity makes it appear and leave at once.
grows out of its neighbour
fades in from 1.2x
appears at once
const [shown, setShown] = useState(true);
<GlassEffectContainer spacing={24} style={{ display: "flex", alignItems: "center", gap: 10, height: 80 }}>
<GlassEffect id="anchor" shape={{ type: "circle" }} width={64} height={64}>
<Star size={24} strokeWidth={2} />
</GlassEffect>
{shown && (
<GlassEffect id="extra" transition="materialize" width={150} height={56}>Hello</GlassEffect>
)}
</GlassEffectContainer>
<Button buttonStyle="glass" onClick={() => setShown(!shown)}>{shown ? "Remove" : "Insert"}</Button>Props
| Prop | Type | Default | Description |
|---|---|---|---|
| spacing | number | 8 | The merge distance: members closer than this melt together. |
| animation | "default" | "smooth" | "snappy" | "bouncy" | { response: number; dampingFraction: number } | { linear: number } | null | "default" | The spring (or linear duration, in seconds) members morph along. null: changes are instant. |
| draggable | boolean | false | Members will be moved directly, every frame (dragged): the glass stays live even at rest, at more cost, so the first touch finds it ready. |
| tintLayer | boolean | false | Members' tints drawn by a filter of their own: fewer passes, for glass that moves with what is under it. |
| margin | number | - | How far the filter layer reaches past the members. By default 40, cut back to the viewport; a fixed value is not cut. |
| className | string | - | On the container element. |
| style | CSSProperties | - | On the container element, for layout. |
| children | ReactNode | - | Members and anything else. |
GlassEffect, inside a container
| Prop | Type | Default | Description |
|---|---|---|---|
| id | string | - | Matches this glass across changes, for morphs. Left out, a member gets an id of its own. |
| union | string | - | Glass of one union id is drawn as one shape over their frames. |
| transition | "matchedGeometry" | "materialize" | "identity" | "matchedGeometry" | How it appears and leaves. |
| standalone | boolean | false | Draw as glass of its own, not merged with the container's. |
SwiftUI
| SwiftUI | @adamexu/glass |
|---|---|
| GlassEffectContainer(spacing: 20) | <GlassEffectContainer spacing={20}> |
| withAnimation(.bouncy) | animation="bouncy" |
| .glassEffectID(id, in: ns) | id={id} |
| .glassEffectUnion(id: u, namespace: ns) | union={u} |
| .glassEffectTransition(.materialize) | transition="materialize" |