"use client"; import * as React from "react"; import { cn } from "../lib/cn"; export type PickerPosition = | "top-left" | "top-center" | "top-right" | "middle-left" | "middle-right" | "bottom-left" | "bottom-center" | "bottom-right"; /** Row by row, as the grid draws them; the centre is no place for a button. */ const GRID: ReadonlyArray = [ "top-left", "top-center", "top-right", "middle-left", null, "middle-right", "bottom-left", "bottom-center", "bottom-right", ]; const PLACES = GRID.filter((place): place is PickerPosition => place !== null); export interface PositionPickerProps { /** The group's name: "Posizione del pulsante". */ label?: string; /** Names the group by an element already on the page, instead of `label`. */ labelledBy?: string; value: PickerPosition; onChange?: (value: PickerPosition) => void; /** Where each square puts the button: "In basso a destra". */ names: Record; /** The choice is shown and read out, and cannot be changed. */ disabled?: boolean; className?: string; } // The design system's PositionPicker: where the widget's button sits on the // customer's site, eight places around the page as a 3 × 3 grid of 44px // squares with the centre empty. The chosen square is navy, the others white. // // One radio group: one tab stop, the arrow keys move between the places and // choose, and each square is named by where it puts the button. function PositionPicker({ label, labelledBy, value, onChange, names, disabled = false, className, }: PositionPickerProps) { const cells = React.useRef(new Map()); const move = (event: React.KeyboardEvent) => { const step = ["ArrowRight", "ArrowDown"].includes(event.key) ? 1 : ["ArrowLeft", "ArrowUp"].includes(event.key) ? -1 : 0; if (step === 0 || disabled) return; event.preventDefault(); const at = PLACES.indexOf(value); const next = PLACES[(at + step + PLACES.length) % PLACES.length]; onChange?.(next); cells.current.get(next)?.focus(); }; return ( // The radios inside take the focus, one tab stop between them. // eslint-disable-next-line jsx-a11y/interactive-supports-focus
{GRID.map((place, index) => place ? (
); } export { PositionPicker };