WindowControlsSpacer
Room at the start of a header for the window's own controls, such as the traffic lights of a macOS window with a hidden title bar. In fullscreen, where macOS hides them, the room goes away.
import { setWindowControlsSource, useWindowControlsInset, WindowControlsSpacer } from '@adecore/ui';import { Segmented, setWindowControlsSource, WindowControlsSpacer } from '@adecore/ui';
import { useEffect, useState } from 'react';
type Window = 'controls' | 'fullscreen';
const listeners = new Set<() => void>();
let inset: number | null = 86;
const show = (window: Window): void => {
inset = window === 'controls' ? 86 : null;
listeners.forEach((listener) => listener());
};
const source = {
inset: () => inset,
subscribe: (onChange: () => void) => {
listeners.add(onChange);
return () => void listeners.delete(onChange);
}
};
export default function WindowControlsSpacerDemo() {
const [window, setWindow] = useState<Window>('controls');
// An app sets its source before the first render; the demo does so before its own, and puts the page's back after.
const [previous] = useState(() => setWindowControlsSource(source));
useEffect(() => () => void setWindowControlsSource(previous), [previous]);
return (
<div className="flex w-full max-w-md flex-col gap-3">
<div className="relative flex h-12 items-center gap-2 rounded-lg border border-border bg-surface pr-3">
{window === 'controls' && (
<span aria-hidden className="pointer-events-none absolute top-1/2 left-4 flex -translate-y-1/2 gap-2">
<span className="size-3 rounded-full bg-text/20" />
<span className="size-3 rounded-full bg-text/20" />
<span className="size-3 rounded-full bg-text/20" />
</span>
)}
<WindowControlsSpacer />
<span className="pl-3 text-sm font-semibold">Inbox</span>
</div>
<Segmented
label="Window"
value={window}
onValueChange={(next) => {
setWindow(next);
show(next);
}}
options={[
{ id: 'controls', label: 'With controls' },
{ id: 'fullscreen', label: 'Fullscreen' }
]}
/>
</div>
);
}Put the spacer first in the header of whatever stands at the left edge: a sidebar, or the WorkspaceTopRow while the sidebar is hidden.
<WorkspaceTopRow leading={!sidebarOpen && <WindowControlsSpacer />}>...</WorkspaceTopRow>useWindowControlsInset() returns the same width, or undefined where there are no controls to clear, for a layout that pads instead of placing a box.
The source
The width comes from a source the app sets once, before the first render. Without one there is no room to keep, as in a browser tab. In a desktop app the shell knows the width; windowControls and followWindowControls report it with every change. Until the shell answers, windowControlsOf gives the room from the platform alone, so the first frame does not move.
import { type WindowControlsState, windowControlsOf } from '@adecore/shell/bridge';
let inset = windowControlsOf(desktop.platform).inset;
const listeners = new Set<() => void>();
const take = (state: WindowControlsState) => {
inset = state.inset;
listeners.forEach((listener) => listener());
};
desktop.onWindowControls(take);
void desktop.windowControls().then(take);
setWindowControlsSource({
inset: () => inset,
subscribe: (onChange) => {
listeners.add(onChange);
return () => listeners.delete(onChange);
}
});setWindowControlsSource returns the source it replaced, so a test can put that one back.
Props
WindowControlsSpacer takes className and ref. WindowControlsSpacerProps and WindowControlsSource are exported types.