"use client"; import * as React from "react"; import { CircleCheckIcon, TriangleAlertIcon } from "lucide-react"; import { useSiteAdmin } from "../context"; import { cn } from "../lib/cn"; import { fill } from "../lib/fill"; import { WIDGET_TOUR_ANCHORS } from "../tour"; import { Card } from "../ui/card"; import { Input } from "../ui/input"; import { Label } from "../ui/label"; import { LauncherGlyph } from "../ui/launcher-glyph"; import { LauncherPicker } from "../ui/launcher-picker"; import { Notice } from "../ui/notice"; import { PositionPicker } from "../ui/position-picker"; import { SegmentedControl } from "../ui/segmented-control"; import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue, } from "../ui/select"; import { Switch } from "../ui/switch"; import { MIN_ICON_CONTRAST, contrastRatio } from "./contrast"; import { UI_PRESET_SIZES, type OffsetUnit, type Size, type UiPosition, } from "./settings-adapter"; import { useWidgetSettings, type WidgetDevice } from "./WidgetSettingsProvider"; const SIZE_IDS: Size[] = ["small", "medium", "large"]; const CUSTOM_SIZE = "custom"; const LANGUAGES = ["auto", "browser", "en", "it", "es"] as const; /** A colour as a swatch that opens the system's picker, and its hex code. */ function ColourField({ id, label, value, onChange, readOnly, }: { id: string; label: string; value: string; onChange: (value: string) => void; readOnly: boolean; }) { const t = useSiteAdmin().messages.widget.colorInput; const picker = React.useRef(null); return (
{label}
); } /** A distance from an edge: a number and its unit. */ function OffsetField({ label, valueLabel, unitLabel, value, unit, onValue, onUnit, readOnly, }: { label: string; valueLabel: string; unitLabel: string; value: number; unit: OffsetUnit; onValue: (value: number) => void; onUnit: (unit: OffsetUnit) => void; readOnly: boolean; }) { return (
{label}
onValue(Number(event.target.value))} aria-label={valueLabel} className="read-only:bg-primary-black-5 relative w-24 rounded-r-none px-3 focus-visible:z-10" />
); } /** Where the mock page puts the button, for each of the eight places. */ const PREVIEW_PLACE: Record = { "top-left": "top-16 left-5", "top-center": "top-16 left-1/2 -translate-x-1/2", "top-right": "top-16 right-5", "middle-left": "top-1/2 left-5 -translate-y-1/2", "middle-right": "top-1/2 right-5 -translate-y-1/2", "bottom-left": "bottom-5 left-5", "bottom-center": "bottom-5 left-1/2 -translate-x-1/2", "bottom-right": "right-5 bottom-5", }; /** * The second tab of the Widget page (ACC-547, board W2): the button's * colours with their contrast, its icon, its size and its place on a computer * and on a phone, and the language of the panel. Beside them a sketch of a * page with the button where it will sit; the real widget is on the dashboard * itself (`WidgetFrame`). */ export function WidgetAppearanceTab() { const { messages, format } = useSiteAdmin(); const t = messages.widget; const { canEdit, config, patch, selectPosition, setOffsetUnit } = useWidgetSettings(); const readOnly = !canEdit; const id = React.useId(); const [device, setDevice] = React.useState("desktop"); // A size typed by hand stays "custom" even when it happens to be a preset. const [customSize, setCustomSize] = React.useState< Record >({ desktop: false, mobile: false }); const desktop = device === "desktop"; const px = desktop ? config.desktopCustomPx : config.mobileCustomPx; const setPx = (value: number) => patch(desktop ? "desktopCustomPx" : "mobileCustomPx", value); const preset = SIZE_IDS.find((size) => UI_PRESET_SIZES[size] === px); const size = customSize[device] || !preset ? CUSTOM_SIZE : preset; const position = desktop ? config.desktopPosition : config.mobilePosition; // Shown or hidden on the device on screen (ACC-695). The widget is never // hidden on both: the last one shown stays on, and the page says why // rather than greying the switch out without a word. const shown = desktop ? config.showOnDesktop : config.showOnMobile; const otherShown = desktop ? config.showOnMobile : config.showOnDesktop; const [hideRefused, setHideRefused] = React.useState(false); const setShown = (checked: boolean) => { if (!checked && !otherShown) { setHideRefused(true); return; } setHideRefused(false); patch(desktop ? "showOnDesktop" : "showOnMobile", checked); }; // The refusal is about the switch that was on screen: it goes with it. const selectDevice = (value: string) => { setHideRefused(false); setDevice(value as WidgetDevice); }; const refused = hideRefused && !otherShown; // The same choice in both cards: which device's settings, and which sketch. const deviceOptions = [ { value: "desktop", label: t.devices.desktop }, { value: "mobile", label: t.devices.mobile }, ]; const hiddenText = desktop ? t.appearance.hiddenOnDesktop : t.appearance.hiddenOnMobile; const ratio = contrastRatio(config.iconColor, config.internalColor); const ratioText = ratio === null ? null : new Intl.NumberFormat(format.intlLocale, { minimumFractionDigits: 1, maximumFractionDigits: 1, }).format(Math.floor(ratio * 10) / 10); const contrastOk = ratio !== null && ratio >= MIN_ICON_CONTRAST; return ( // A column that can shrink below its content on a phone too: an implicit // one is as wide as the widest thing in the card.

{t.appearance.colorsTitle}

patch("iconColor", value)} readOnly={readOnly} /> patch("internalColor", value)} readOnly={readOnly} />

{ratioText !== null && ( <> {contrastOk ? (

{t.appearance.buttonTitle}

patch("iconStyle", value)} color={config.iconColor} glyph={config.internalColor} names={t.iconStylePicker.styles} disabled={readOnly} /> {/* Not switched off for a reader: it changes what is shown, not what is saved, and the phone's settings are theirs to read. */}
{/* On the page before it has anything to say: a live region that arrives with its words already in it is not always read. The notice gives up its own role to it. */}
{refused && ( {t.appearance.showLastHint} )}
{t.appearance.launcherSize} { const custom = value === CUSTOM_SIZE; setCustomSize((prev) => ({ ...prev, [device]: custom })); if (!custom) setPx(UI_PRESET_SIZES[value as Size]); }} options={[ ...SIZE_IDS.map((value) => ({ value, label: t.appearance.sizes[value], })), { value: CUSTOM_SIZE, label: t.sizePicker.customSize }, ]} /> {size === CUSTOM_SIZE && (
setPx(Number(event.target.value))} aria-describedby={`${id}-size-hint`} className="read-only:bg-primary-black-5 w-24 px-3" />

{t.sizePicker.customSizeHint}

)}
{t.appearance.position}
selectPosition(device, value)} names={t.positionPicker.positions} disabled={readOnly} />
patch(desktop ? "desktopOffsetH" : "mobileOffsetH", value) } onUnit={(unit) => setOffsetUnit(device, "H", unit)} readOnly={readOnly} /> patch(desktop ? "desktopOffsetV" : "mobileOffsetV", value) } onUnit={(unit) => setOffsetUnit(device, "V", unit)} readOnly={readOnly} />

{t.language.hint}

{/* Sticky beside the settings from 1024px (ACC-586, Widget_2): every change shows without scrolling back up to the preview. `items-start` on the grid gives it the room to travel. Not on a window too short to hold it: what does not fit could never be scrolled into view. */}

{t.appearance.previewTitle}

{shown && ( )}
{/* Outside the sketch, which is one image to a screen reader: its name already says this, so the caption is for the eyes only. */} {!shown && ( )}
); }