GlassProvider
Settings for every glass inside it: the colour scheme, the Liquid Glass tint amount and the device idiom. Glass outside any provider uses the defaults.
import { GlassProvider } from "@adamexu/glass";Setup
Wrap the app. Every prop is optional. There is no stylesheet to import: each component imports its own CSS, and the Inter it draws with ships with the package.
import { GlassProvider } from "@adamexu/glass";
export function App({ children }) {
return <GlassProvider>{children}</GlassProvider>;
}Colour scheme
scheme is light or dark, and glass draws for it whatever the page around it does. It does not follow prefers-color-scheme by itself; to follow the system, pass it in:
const dark = useSyncExternalStore(
(changed) => {
const query = matchMedia("(prefers-color-scheme: dark)");
query.addEventListener("change", changed);
return () => query.removeEventListener("change", changed);
},
() => matchMedia("(prefers-color-scheme: dark)").matches,
() => false,
);
<GlassProvider scheme={dark ? "dark" : "light"}>{children}</GlassProvider>Tint amount
The iOS setting under Liquid Glass: 0 is clear, 1 is tinted, and 0.5 is the iOS default. Drag the slider to change it for the glass below.
const [amount, setAmount] = useState(0.5);
<Slider aria-label="Tint amount" value={amount} onChange={setAmount} />
<GlassProvider scheme={scheme} tintAmount={amount}>
<Button buttonStyle="glass">Cancel</Button>
<Button buttonStyle="glassProminent" tint="#ff5a1f">Continue</Button>
<Toggle aria-label="Example" defaultOn />
</GlassProvider>Props
| Prop | Type | Default | Description |
|---|---|---|---|
| scheme | "light" | "dark" | "light" | The colour scheme glass and controls draw for, as SwiftUI's preferredColorScheme. |
| tintAmount | number | 0.5 | Settings > Liquid Glass > Tint Amount: 0 (most clear) to 1 (most tinted). 0.5 is the iOS default. |
| idiom | "auto" | "phone" | "pad" | "duo" | "auto" | The device, for what differs by it: how far a finger can slide off a pressed button before release cancels, and NavigationStack's layout. auto: a window under 668 px wide is a phone, otherwise an iPad. Controls with an idiom prop of their own take that instead. |
| children | ReactNode | - | Everything that uses these settings. |
useGlassSettings
useGlassSettings(): GlassSettings returns the settings of the nearest provider, or the defaults outside one: an object with scheme, tintAmount and idiom. Use it to draw your own content to match the glass around it.
function Settings() {
const { scheme, tintAmount, idiom } = useGlassSettings();
return <span>{`{ scheme: "${scheme}", tintAmount: ${tintAmount}, idiom: "${idiom}" }`}</span>;
}Content Security Policy
Glass maps are built in Web Workers that the components start from their own module files, and drawn as data: images inside SVG filters. A strict policy needs:
img-src data:for the maps.worker-src 'self', or wherever your bundler serves the worker files it emits next to the package's modules.
When a worker cannot start, its maps are built on the main thread: the glass is identical, and first paints are slower.
Browser support
- Glass is Chromium only: Chrome, Edge, Arc, Brave, Opera, Android Chrome and Electron. Other browsers are not supported, and there is no fallback.
- Glass draws on the client once its maps are built, so the server render and the hydrating render show the same box and content.
SwiftUI
| SwiftUI | @adamexu/glass |
|---|---|
| Settings > Liquid Glass > Tint Amount | <GlassProvider tintAmount> |
| .preferredColorScheme(.dark) | <GlassProvider scheme="dark"> |
| @Environment(\.colorScheme) | useGlassSettings().scheme |