An open-source, self-hosted system for apps built with web technologies.
import { useLayoutEffect, useRef, useState, type CSSProperties, type ReactNode } from "react"
import { Surface, Toolbar, type SurfaceProps, useTiming } from "@phreshos/react-ui"
import { type AppearanceTaskbar, type TaskbarPosition } from "@phreshos/core"
import { useReducedMotion } from "@libs/react-motion"
import { cssEasing } from "@phreshos/core"
/**
* The Desktop's Taskbar is a Shell entity; React UI's Toolbar supplies the
* control-group semantics and orientation-aware keyboard navigation.
*/
export default function Taskbar({ leading, navigation, trailing, taskbar, spacing, keepVisible = false, className, style, children, onPointerEnter, "aria-label": label = "Taskbar", ...props }: TaskbarProps) {
const horizontal = taskbar.position === "top" || taskbar.position === "bottom"
const orientation = horizontal ? "horizontal" : "vertical"
// Every section stands the same distance from the separators beside it, and across a vertical Taskbar its buttons share one width.
const room = spacing / 8
const section = (before: boolean, after: boolean): CSSProperties => ({
flexShrink: 0,
minWidth: 0,
alignItems: horizontal ? "center" : "stretch",
alignSelf: horizontal ? "center" : "stretch",
[horizontal ? "paddingInlineStart" : "paddingBlockStart"]: before ? room : 0,
[horizontal ? "paddingInlineEnd" : "paddingBlockEnd"]: after ? room : 0
})
const transaction = useTiming()("change")
const reducedMotion = useReducedMotion()
const [revealed, setRevealed] = useState(false)
const configuration = { position: taskbar.position, overlay: taskbar.overlay }
const previousConfiguration = useRef(configuration)
const configurationChanged = taskbarConfigurationChanged(previousConfiguration.current, configuration)
const visible = taskbarVisible(taskbar, revealed, keepVisible, configurationChanged)
useLayoutEffect(() => {
if (!configurationChanged) return
// A reveal belongs to the edge that detected the pointer. Carrying it
// across an Appearance relocation paints one visible frame at the new edge.
previousConfiguration.current = configuration
setRevealed(false)
}, [configurationChanged, taskbar.overlay, taskbar.position])
return <div
data-taskbar-region=""
className="pointer-events-none absolute"
style={taskbarRegionStyle(taskbar, spacing)}
onPointerLeave={() => setRevealed(false)}
>
{taskbar.overlay && visible ? <div
data-taskbar-retention-region=""
aria-hidden="true"
className="pointer-events-auto absolute inset-0"
/> : null}
{taskbar.overlay ? <div
data-taskbar-reveal-region=""
aria-hidden="true"
className="pointer-events-auto absolute"
style={taskbarRevealRegionStyle(taskbar.position, spacing)}
onPointerEnter={() => setRevealed(true)}
/> : null}
<Surface
as={Toolbar}
aria-label={label}
orientation={orientation}
gap="small"
data-orientation={orientation}
className={`pointer-events-auto isolate min-h-0 min-w-0 overflow-hidden p-1.5 ${className ?? ""}`}
style={{
...taskbarStyle(taskbar, spacing),
alignItems: "center",
transform: taskbarOverlayTransform(taskbar.position, visible, spacing),
transitionDuration: reducedMotion || configurationChanged ? "0ms" : `${transaction.duration}ms`,
transitionTimingFunction: cssEasing(transaction.easing),
transitionProperty: "transform",
...style
}}
{...props}
material="frosted"
onPointerEnter={event => {
setRevealed(true)
onPointerEnter?.(event)
}}
>
<Toolbar.Group style={section(false, true)}>{leading}</Toolbar.Group>
<Toolbar.Separator className="shadow-taskbar-separator" />
{/* Where this Desktop looks, between what starts Programs and the Windows they open. */}
{navigation && <>
<Toolbar.Group style={section(true, true)}>{navigation}</Toolbar.Group>
<Toolbar.Separator className="shadow-taskbar-separator" />
</>}
{/* Clipped only along the Taskbar, and a little past its ends, so the items' shadows and focus show whole. */}
<Toolbar.Group style={{ ...section(true, true), flex: "1 1 0", alignSelf: "stretch", minHeight: 0, overflowClipMargin: spacing / 4, ...(horizontal ? { overflowX: "clip" } : { overflowY: "clip" }) }}>
{children}
</Toolbar.Group>
<Toolbar.Separator className="shadow-taskbar-separator" />
<Toolbar.Group style={section(true, false)}>{trailing}</Toolbar.Group>
</Surface>
</div>
}
interface TaskbarProps extends Omit<SurfaceProps<typeof Toolbar>, "as" | "children" | "orientation"> {
children: ReactNode
leading: ReactNode
/** Where this Desktop looks on the plane of standard Windows, a section of its own before the Windows. */
navigation?: ReactNode
trailing: ReactNode
taskbar: AppearanceTaskbar
spacing: number
/** Retains an overlay Taskbar while an anchored Shell surface is open. */
keepVisible?: boolean
}
/** Taskbar size is its cross-axis extent; spacing remains its outer margin. */
export function taskbarStyle(taskbar: AppearanceTaskbar, spacing: number): CSSProperties {
const horizontal = taskbar.position === "top" || taskbar.position === "bottom"
return {
position: "absolute",
[taskbar.position]: spacing,
...(horizontal
? { left: spacing, right: spacing, width: "auto", height: taskbar.size }
: { top: spacing, bottom: spacing, width: taskbar.size, height: "auto" })
}
}
/** Bounds shared by the edge sensor and the revealed Taskbar. */
export function taskbarRegionStyle(taskbar: AppearanceTaskbar, spacing: number): CSSProperties {
const horizontal = taskbar.position === "top" || taskbar.position === "bottom"
return {
[taskbar.position]: 0,
...(horizontal
? { left: 0, right: 0, height: taskbar.size + spacing + taskbarRetentionDistance }
: { top: 0, bottom: 0, width: taskbar.size + spacing + taskbarRetentionDistance })
}
}
/** Fixed inward distance a revealed overlay Taskbar retains pointer ownership. */
export const taskbarRetentionDistance = 50
interface TaskbarVisibilityConfiguration {
position: TaskbarPosition
overlay: boolean
}
export function taskbarConfigurationChanged(previous: TaskbarVisibilityConfiguration, current: TaskbarVisibilityConfiguration) {
return previous.position !== current.position || previous.overlay !== current.overlay
}
export function taskbarVisible(taskbar: AppearanceTaskbar, revealed: boolean, keepVisible: boolean, configurationChanged: boolean) {
return !taskbar.overlay || keepVisible || revealed && !configurationChanged
}
/** The outer Appearance spacing is the non-obstructing reveal target. */
export function taskbarRevealRegionStyle(position: TaskbarPosition, spacing: number): CSSProperties {
const horizontal = position === "top" || position === "bottom"
return {
[position]: 0,
...(horizontal
? { left: 0, right: 0, height: spacing }
: { top: 0, bottom: 0, width: spacing })
}
}
/** Moves an overlay Taskbar wholly beyond its configured Desktop edge. */
export function taskbarOverlayTransform(position: TaskbarPosition, visible: boolean, spacing: number) {
if (visible) return "translate(0)"
if (position === "top") return `translateY(calc(-100% - ${spacing}px))`
if (position === "bottom") return `translateY(calc(100% + ${spacing}px))`
if (position === "left") return `translateX(calc(-100% - ${spacing}px))`
return `translateX(calc(100% + ${spacing}px))`
}