diff --git a/AGENTS.md b/AGENTS.md index c4c3ebc..26848a0 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -86,6 +86,9 @@ headless/ solidtype-headless: the components Kobalte lacks, one date_picker a date field whose group's trigger opens a popover of a calendar choosing its date or its range, a time kept, as React Stately's pickers keep them + command a search that filters a menu of commands, the focus + kept in the search while the keys move through those + shown, as React Aria's autocomplete filters its menu color_picker one colour its parts share, as React Aria's: the colour components below read it, given no value of their own; it draws nothing @@ -150,7 +153,7 @@ heroui/ solidtype-heroui: HeroUI's components ported onto selection_indicator what a tab's and a segment's indicator share: drawn in the selected item, it starts where the last one was - segment trend_chip text_shimmer number_stepper + segment trend_chip text_shimmer number_stepper command components HeroUI ships no stylesheet for, made as every port is: their look a stylesheet each table.css what HeroUI's table stylesheet does not draw: a column @@ -534,14 +537,18 @@ break. its component and its check: its own options, then its children by kind, then its element's attributes' `check`, an `external` nothing calls. Its options map as an element's attributes do, and further: a - number is a `float`, since a slider's value is fractional; `null` is - `Js.Nullable.t`, since Kobalte reads `undefined` as uncontrolled and - `null` as controlled and empty; `undefined` is absence for an optional - prop, an optional argument, `?name`, then `()`, for a function the user - calls, and an `option` anywhere else; members of different JavaScript - kinds are `Types.Either.t`, built by `left` and `right`, which is each - value as it is; an option accessor that takes a key or a function - takes the function, and one that takes only a key, + number is a `float`, since a slider's value is fractional; `T | null` + given to Kobalte -- an option, an argument, what a callback returns -- is + `Js.Null.t`, since Kobalte reads `undefined` as uncontrolled and `null` + as controlled and empty, and `Js.Null.t` cannot hold `undefined`, so an + `option` becomes one only by `Js.Null.fromOption`, whose `None` is + `null`; read from Kobalte, or `T | null | undefined` given, it is + `Js.Nullable.t`, which takes whatever JavaScript holds; `undefined` is + absence for an optional prop, an optional argument, `?name`, then `()`, + for a function the user calls, and an `option` anywhere else; members + of different JavaScript kinds are `Types.Either.t`, built by `left` and + `right`, which is each value as it is; an option accessor that takes a + key or a function takes the function, and one that takes only a key, `optionGroupChildren`, a `string`; a root whose value turns on `multiple` is two modules, `Root` and `Root_multiple`; a named object type is a module of `Types`, with a getter per field, a curried @@ -564,8 +571,10 @@ break. hooks its lists are made of are, from its root, as `Selection`. Why: Kobalte's parts are hundreds, and a binding by hand would be a second copy of its types; a component Kobalte lacks is made of its hooks. Test: - `make lint` runs the generator with `--check`, and - `test/kobalte/selection_test.mlx` makes a list of them. + `make lint` runs the generator with `--check`, + `test/kobalte/selection_test.mlx` makes a list of them, and + `test/does_not_compile.t` refuses `Js.Nullable.fromOption` where + `T | null` is given. - **HeroUI's variant functions are generated from @heroui/styles' declarations**, by `generate/heroui.ts` into `heroui/styles.ml` and its `.mli`, on the same core as Kobalte's: a module per function, diff --git a/CHANGES.md b/CHANGES.md index 3cafa84..faa6463 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -1,5 +1,39 @@ # Changes +## 0.14.0 (2026-11-22) + +- Breaking: a value that may be empty is given as `Js.Null.t`, never + `Js.Nullable.t`: a Kobalte prop or argument TypeScript types `T | null`, + and the value of `Date_field`, `Time_field`, `Date_picker`, + `Date_range_picker`, `Calendar`, `Range_calendar` and `Color_field`, in + `solidtype-headless` and `solidtype-heroui`. `Js.Null.fromOption None` is + `null`, controlled and empty; `Js.Nullable.fromOption None`, which the + documented examples wrote, is the `undefined` a component reads as no + value given, and no longer compiles there. What Kobalte tells, an + `on_change`'s argument, stays `Js.Nullable.t`. +- Breaking: `Radio_group`'s `value` is a `string Js.Null.t`, and `null` + controls it with none chosen, as React's does. +- `Command`, a command palette in HeroUI's look with a stylesheet of its own: + a search in a dialog that filters a menu of commands, kept or controlled, + by its text or a `filter` given. The focus stays in the search while the + arrows move through the commands shown, wrapping, and Enter or a press + runs one; typing focuses the first command shown, and Escape empties the + search before it closes the palette. Groups hide with their commands, a + separator with nothing to part, and the list draws `render_empty_state` + while it shows none; `sm`, `md` and `lg` sizes, and opaque, blur and + transparent backdrops. `Solidtype_headless.Command` is the palette itself, + of Kobalte's selection hooks, its words React Aria's in 34 locales. +- A `Date_picker`, `Date_range_picker`, `Calendar` or `Range_calendar` set + to `null` after a date was typed or chosen is empty, where it kept the + last date and sent it with a form. +- `Number_field` set to `nan` is empty, as React's is, where it kept the + last number shown; `Solidtype_headless.Number_field` fixes Kobalte's. Its + `on_change` tells typed text once Enter or a focus that leaves commits + it, as React's does, where it told every key. +- A `Date_range_picker`'s arrows, Backspace and a segment typed whole move + between its start and its end, as React's do; so does + `Solidtype_headless.Date_field.Root_range`'s. + ## 0.13.0 (2026-11-22) - Breaking: the skill, `skills/solidtype`, is no longer installed. The diff --git a/agents/llms.txt b/agents/llms.txt index 1cb7c28..1efc645 100644 --- a/agents/llms.txt +++ b/agents/llms.txt @@ -143,6 +143,7 @@ Each component's page gives HeroUI's own examples in OCaml, and a table of its p - [AvatarGroup](pages/heroui/avatar-group.md): Display a stacked or grid group of avatars with overflow counting. - [Accordion](pages/heroui/accordion.md): A collapsible content panel for organizing information in a compact space. - [Breadcrumbs](pages/heroui/breadcrumbs.md): Navigation breadcrumbs showing the current page's location within a hierarchy. +- [Command](pages/heroui/command.md): A command palette, a search that filters a menu of commands in a dialog. - [Disclosure](pages/heroui/disclosure.md): A disclosure is a collapsible section with a header containing a heading and a trigger button, and a panel that wraps the content. - [DisclosureGroup](pages/heroui/disclosure-group.md): Container that manages multiple Disclosure items with coordinated expanded states. - [Link](pages/heroui/link.md): A styled anchor component for navigation with built-in icon support. diff --git a/agents/pages/heroui/calendar.md b/agents/pages/heroui/calendar.md index ca54c98..38578ff 100644 --- a/agents/pages/heroui/calendar.md +++ b/agents/pages/heroui/calendar.md @@ -250,7 +250,7 @@ let make () = S.set set_value (Some d)) on_focus_change=(S.set set_focused)> @@ -834,7 +834,7 @@ let make () = aria_label="Booking date" is_date_unavailable min_value=(D.today (D.get_local_time_zone ())) - value=(Js.Nullable.fromOption (S.get selected)) + value=(Js.Null.fromOption (S.get selected)) on_change=(fun d -> S.set set_selected (Some d))> @@ -1034,7 +1034,7 @@ Each prop beside the name HeroUI's React component gives it. | React | solidtype | Type | Default | Description | |---|---|---|---|---| -| `value` | `value` | `CalendarDate.t Js.Nullable.t` | | Controlled selected date; null controls it empty | +| `value` | `value` | `CalendarDate.t Js.Null.t` | | Controlled selected date; null controls it empty | | `defaultValue` | `default_value` | `CalendarDate.t` | | Initial selected date (uncontrolled) | | `onChange` | `on_change` | `CalendarDate.t -> unit` | | Called when a date is chosen | | `selectionMode` | — | | | `Calendar.Multiple` chooses several dates | diff --git a/agents/pages/heroui/color-field.md b/agents/pages/heroui/color-field.md index 70a95ab..7e1bcf5 100644 --- a/agents/pages/heroui/color-field.md +++ b/agents/pages/heroui/color-field.md @@ -34,7 +34,7 @@ let make () = @@ -338,7 +338,7 @@ let make () = class_="w-[100px]" color_space=`hsl name - value=(Js.Nullable.fromOption (S.get color)) + value=(Js.Null.fromOption (S.get color)) on_change=(S.set set_color)> @@ -404,7 +404,7 @@ let make () = @@ -491,7 +491,7 @@ let make () = is_required=true class_="w-full" name="brand-color" - value=(Js.Nullable.fromOption (S.get value)) + value=(Js.Null.fromOption (S.get value)) on_change=(S.set set_value)> @@ -544,7 +544,7 @@ let make () = "Applied to buttons, links, and focus rings." @@ -613,7 +613,7 @@ Each prop beside the name HeroUI's React component gives it. | React | solidtype | Type | Default | Description | |---|---|---|---|---| -| `value` | `value` | `Types.Color.t Js.Nullable.t` | | Current value (controlled), null for none | +| `value` | `value` | `Types.Color.t Js.Null.t` | | Current value (controlled), null for none | | `defaultValue` | `default_value` | `Types.Color.t` | | Default value (uncontrolled) | | `onChange` | `on_change` | `Types.Color.t option -> unit` | | Handler called when the value changes, `None` for none | | `channel` | `channel` | `Color_field.channel` | | The color channel to edit, fixed as it is made. If not provided, edits hex value | diff --git a/agents/pages/heroui/command.md b/agents/pages/heroui/command.md new file mode 100644 index 0000000..e7cf750 --- /dev/null +++ b/agents/pages/heroui/command.md @@ -0,0 +1,1012 @@ +# Command + +A command palette, a search that filters a menu of commands in a dialog. + +A command palette: a search that filters a menu of commands, in a dialog. The +focus stays in the search while the arrow keys move through the commands +shown; Enter or a press runs one. Its look is a stylesheet the library ships, +imported as [Theming](theming.md#setup) shows. + +## Usage + +```ocaml title="command_usage.mlx" +module Jsx = Solidtype.Jsx +module Button = Solidtype_heroui.Button +module Chip = Solidtype_heroui.Chip +module Command = Solidtype_heroui.Command +module Kbd = Solidtype_heroui.Kbd +module Gravity = Solidtype_gravity_icons + +let make () = + + + + + + + "Home" + + + + + + + + + "Esc" + + + + + + "Create new file..." + + + "Create new folder..." + + + "Assign to me" + + + + + "Preferences" + + + "Change theme" + + + "Keyboard shortcuts" + + + + +
+
+
+ + +
+ "Navigate" +
+
+ "Select" +
+
+
+
+
+
+
+``` + +## Anatomy + +```ocaml + + + + + + + + + + + + + + + + + + + + + + + +``` + +A `Button` inside `Command` opens it. Typing shows the commands whose text +holds what is typed, and focuses the first. Escape empties the search, then +closes the palette. The palette stays open as a command runs: close it in +`on_action` where it should. + +## Examples + +### Clean + +```ocaml title="command_clean.mlx" +module Jsx = Solidtype.Jsx +module For = Solidtype.For +module Button = Solidtype_heroui.Button +module Command = Solidtype_heroui.Command +module Kbd = Solidtype_heroui.Kbd +module Gravity = Solidtype_gravity_icons + +let keys shortcut = +
+ + (fun key _ -> + + (Jsx.string key) + ) + +
+ +let make () = + + + + + + + + "home" + + + + + + + + + + + "Search Projects..." + (keys [| "S"; "P" |]) + + + "Create New Project..." + + + + + + "Search Teams..." + (keys [| {js|⇧|js}; "P" |]) + + + "Create New Team..." + + + + + + "Search Docs..." + (keys [| {js|⇧|js}; "D" |]) + + + "Send Feedback..." + + + "Contact Support" + + + + + + + +``` + +### Dev Toolbar + +```ocaml title="command_dev_toolbar.mlx" +module Jsx = Solidtype.Jsx +module Button = Solidtype_heroui.Button +module Command = Solidtype_heroui.Command +module Kbd = Solidtype_heroui.Kbd +module Gravity = Solidtype_gravity_icons + +let shortcut key = + + (Jsx.string key) + + +let make () = + + + + + + + + + + + + + "Esc" + + + + + + +
+ "View Environment" + + "staging/feat-dashboard-redesign" + +
+
+
+ + + "Feedback" (shortcut "C") + + + "Notifications" (shortcut "N") + + + "Feature Flags..." (shortcut "F") + + + "Share Preview" (shortcut "S") + + + + "Switch Branch..." + (shortcut "B") + + + + + "View Logs" (shortcut "L") + + + "Tracing..." (shortcut "T") + + +
+
+
+
+
+``` + +### Launcher + +```ocaml title="command_launcher.mlx" +module Jsx = Solidtype.Jsx +module For = Solidtype.For +module Button = Solidtype_heroui.Button +module Command = Solidtype_heroui.Command +module Kbd = Solidtype_heroui.Kbd +module Gravity = Solidtype_gravity_icons + +(* An icon on a square of its colour, and what kind of entry it is. *) +let entry ~colour ~icon ~kind name = + +
+ icon +
+ (Jsx.string name) + (Jsx.string kind) +
+ +let hint key = + + (Jsx.string key) + + +let make () = + + + + + + + + + + + + (entry ~colour:"bg-violet-500" ~icon: + ~kind:"Application" "Design Tool") + (entry ~colour:"bg-indigo-500" ~icon: + ~kind:"Application" "Project Tracker") + (entry ~colour:"bg-sky-500" ~icon: + ~kind:"Application" "Team Chat") + (entry ~colour:"bg-red-500" ~icon: + ~kind:"Application" "Video Platform") + (entry ~colour:"bg-amber-500" ~icon: + ~kind:"Application" "Notes") + + + (entry + ~colour:"rounded-md bg-gradient-to-b from-red-400 to-red-600" + ~icon: ~kind:"Command" "Clipboard History") + (entry + ~colour: + "rounded-md bg-gradient-to-b from-emerald-400 to-emerald-600" + ~icon: ~kind:"Command" "Import Extension") + (entry + ~colour: + "rounded-md bg-gradient-to-b from-emerald-400 to-emerald-600" + ~icon: ~kind:"Command" "Manage Extensions") + + + +
+ "Open Application" + (hint {js|↵|js}) +
+
+
+ "Actions" +
(hint {js|⌘|js}) (hint "K")
+
+ + + + + +``` + +### Minimal + +```ocaml title="command_minimal.mlx" +module Jsx = Solidtype.Jsx +module For = Solidtype.For +module Button = Solidtype_heroui.Button +module Command = Solidtype_heroui.Command +module Kbd = Solidtype_heroui.Kbd +module Gravity = Solidtype_gravity_icons + +(* A command marked by a bar at its start while it is focused, its keys + beside it. *) +let command ~icon ~keys name = + + icon + (Jsx.string name) +
+ + (fun key _ -> + (Jsx.string key)) + +
+
+ +let make () = + + + + + + +
+ "Issue - FUN-343" +
+
+ + + + + + (command ~icon: ~keys:[| "A" |] "Assign to...") + (command ~icon: ~keys:[| "I" |] "Assign to me") + (command ~icon: ~keys:[| "S" |] + "Change status...") + (command ~icon: ~keys:[| "P" |] + "Change priority...") + (command ~icon: ~keys:[| "L" |] "Change labels...") + (command ~icon: ~keys:[| {js|⇧|js}; "L" |] + "Remove label...") + (command ~icon: ~keys:[| {js|⇧|js}; "D" |] + "Set due date...") + +
+
+
+
+``` + +### Split View + +`Command.Context.use ()`, called in a component inside the dialog, gives the +focused command's key, read with `S.get`: here it picks the preview. + +```ocaml title="command_split_view.mlx" +module S = Solidtype.Solid +module Jsx = Solidtype.Jsx +module For = Solidtype.For +module Avatar = Solidtype_heroui.Avatar +module Button = Solidtype_heroui.Button +module Chip = Solidtype_heroui.Chip +module Command = Solidtype_heroui.Command +module Input = Solidtype_heroui.Input +module Kbd = Solidtype_heroui.Kbd +module Label = Solidtype_heroui.Label +module Radio = Solidtype_heroui.Radio +module Radio_group = Solidtype_heroui.Radio_group +module Slider = Solidtype_heroui.Slider +module Switch = Solidtype_heroui.Switch +module Gravity = Solidtype_gravity_icons + +let components = + [| + ("Button", "Trigger actions", fun () -> ); + ("Input", "Retrieve user input", fun () -> ); + ("Radio", "Single choice input", fun () -> ); + ("Chip", "Annotate context", fun () -> ); + ("Slider", "Free range picker", fun () -> ); + ("Avatar", "Illustrate the user", fun () -> ); + ("Switch", "Toggle a setting", fun () -> ); + |] + +(* The component the focused item names, drawn beside the list. *) +module Preview = struct + let%component make () = + let palette = Command.Context.use () in +
+ (match S.get palette.focused_key with + | Some "Input" -> + + | Some "Radio" -> + + + + "Basic" + + + + + "Premium" + + + + | Some "Chip" -> "Chip" + | Some "Slider" -> + + + + + | Some "Avatar" -> + "JD" + | Some "Switch" -> + + + + + + + | Some _ | None -> ) +
+end + +let make () = + + + + + + + + + + + + +
+ + + + (fun (name, purpose, icon) _ -> + +
+ (icon ()) +
+
+ + (Jsx.string name) + + + (Jsx.string purpose) + +
+
) +
+
+
+
+ +
+ + + + +``` + +### Multiple Search Terms + +`filter` decides which commands are shown, given a command's text and the +search's: here every word typed must be in it. + +```ocaml title="command_multiple_search_terms.mlx" +module S = Solidtype.Solid +module Jsx = Solidtype.Jsx +module For = Solidtype.For +module Button = Solidtype_heroui.Button +module Chip = Solidtype_heroui.Chip +module Close_button = Solidtype_heroui.Close_button +module Command = Solidtype_heroui.Command +module Kbd = Solidtype_heroui.Kbd +module Gravity = Solidtype_gravity_icons + +(* An item is shown while every word typed is in its text, in any order. *) +let every_term text typed = + let text = String.lowercase_ascii text in + String.split_on_char ' ' (String.lowercase_ascii typed) + |> List.filter (fun term -> not (String.equal term "")) + |> List.for_all (fun term -> + let n = String.length term and m = String.length text in + let rec from i = + i + n <= m && (String.equal (String.sub text i n) term || from (i + 1)) + in + from 0) + +let make () = + let scopes, set_scopes = + S.create_signal [| "Projects"; "Tasks"; "People"; "Documents"; "Channels" |] + in + + + + + + + + + + + + + + "K" + + + + +
+ + (fun scope _ -> + + (Jsx.string scope) + + S.update set_scopes + (Js.Array.filter ~f:(fun s -> + not (String.equal s scope)))) + /> + ) + +
+
+ + + + + "Summarize this week's progress" + + + "Create a task for the team" + + + "Draft a project brief" + + + "Schedule a standup meeting" + + + + "Results (4)" + + )> + + "View recent activity" + + + "Open project roadmap" + + + "Browse team directory" + + + "Manage workspace settings" + + + + +
+
+
+ + +
+ "Navigate" +
+
+ "Select" +
+
+ + "Not what you're looking for? Try the " + + +
+
+
+
+
+``` + +### Sizes + +```ocaml title="command_sizes.mlx" +module Jsx = Solidtype.Jsx +module For = Solidtype.For +module Button = Solidtype_heroui.Button +module Chip = Solidtype_heroui.Chip +module Command = Solidtype_heroui.Command +module Kbd = Solidtype_heroui.Kbd +module Gravity = Solidtype_gravity_icons + +let sizes = [| (`sm, "sm"); (`md, "md"); (`lg, "lg") |] + +let make () = +
+ + (fun (size, name) _ -> + + + + + + + "Home" + + + + + + + + + "Esc" + + + + + + "Create new file..." + + + + "Create new folder..." + + + "Assign to me" + + + + + "Preferences" + + + "Change theme" + + + "Keyboard shortcuts" + + + + +
+
+
+ + +
+ "Navigate" +
+
+ + "Select" +
+
+
+
+
+
+
) +
+
+``` + +### Backdrop Variants + +```ocaml title="command_backdrop_variants.mlx" +module Jsx = Solidtype.Jsx +module For = Solidtype.For +module Button = Solidtype_heroui.Button +module Chip = Solidtype_heroui.Chip +module Command = Solidtype_heroui.Command +module Kbd = Solidtype_heroui.Kbd +module Gravity = Solidtype_gravity_icons + +let variants = + [| (`transparent, "transparent"); (`opaque, "opaque"); (`blur, "blur") |] + +let make () = +
+ + (fun (variant, name) _ -> + + + + + + + "Home" + + + + + + + + + "Esc" + + + + + + "Create new file..." + + + + "Create new folder..." + + + "Assign to me" + + + + + "Preferences" + + + "Change theme" + + + "Keyboard shortcuts" + + + + +
+
+
+ + +
+ "Navigate" +
+
+ + "Select" +
+
+
+
+
+
+
) +
+
+``` + +## Styling Reference + +### CSS Classes + +- `.command__backdrop`, and its variants `.command__backdrop--transparent`, + `.command__backdrop--opaque`, `.command__backdrop--blur` +- `.command__container` +- `.command__dialog`, and its sizes `.command__dialog--sm`, + `.command__dialog--md`, `.command__dialog--lg` +- `.command__header` +- `.command__input-group`, `.command__input-group-prefix`, + `.command__input-group-input`, `.command__input-group-clear-button`, + `.command__input-group-suffix` +- `.command__list`, `.command__empty` +- `.command__group`, `.command__group-heading` +- `.command__item` +- `.command__separator` +- `.command__footer` + +### Interactive States + +- **Entering**, **Exiting**: `[data-entering="true"]`, `[data-exiting="true"]` + on `.command__backdrop` and `.command__container` +- **Focused**: `[data-focused="true"]` on `.command__item`, the command the keys + or the pointer reached +- **Hovered**: `[data-hovered="true"]` on `.command__item` +- **Pressed**: `[data-pressed="true"]` on `.command__item` +- **Disabled**: `[data-disabled="true"]` on `.command__item` +- **Empty**: `[data-empty="true"]` on `.command__input-group` while the search + is, which hides its clear button + +## API Reference + +Each prop beside its name in React's convention. + +### Command + +| React | solidtype | Type | Default | Description | +|---|---|---|---|---| +| `isOpen` | `is_open` | `bool` | | Whether it is open (controlled) | +| `defaultOpen` | `default_open` | `bool` | | Whether it is open at first (uncontrolled) | +| `onOpenChange` | `on_open_change` | `bool -> unit` | | Called as it opens and closes | +| `children` | `children` | elements | | Its trigger and its backdrop | + +### Command.Backdrop + +| React | solidtype | Type | Default | Description | +|---|---|---|---|---| +| `variant` | `variant` | `` [ `opaque \| `blur \| `transparent ] `` | `` `opaque `` | How it covers the page | +| `isDismissable` | `is_dismissable` | `bool` | `true` | Whether a press on it closes the palette | +| `isKeyboardDismissDisabled` | `is_keyboard_dismiss_disabled` | `bool` | `false` | Whether Escape in an empty search leaves it open | +| `isOpen` | `is_open` | `bool` | | Whether it is open, with no `Command` around it | +| `defaultOpen` | `default_open` | `bool` | | Whether it is open at first, with no `Command` around it | +| `onOpenChange` | `on_open_change` | `bool -> unit` | | Called as it opens and closes, with no `Command` around it | +| `UNSTABLE_portalContainer` | `portal_container` | element | | Where it is drawn, else the page's body | +| `className` | `class_` | `string` | | Added after its own classes | + +### Command.Container + +| React | solidtype | Type | Default | Description | +|---|---|---|---|---| +| `size` | `size` | `` [ `sm \| `md \| `lg ] `` | `` `md `` | The palette's width and greatest height | +| `className` | `class_` | `string` | | Added after its own classes | + +### Command.Dialog + +| React | solidtype | Type | Default | Description | +|---|---|---|---|---| +| `aria-label` | `aria_label` | `string` | `"Command palette"` | Its name | +| `inputValue` | `input_value` | `string` | | The search's text (controlled) | +| `defaultInputValue` | `default_input_value` | `string` | | The search's text at first (uncontrolled) | +| `onInputChange` | `on_input_change` | `string -> unit` | | Called with the search's text as it changes | +| `filter` | `filter` | `string -> string -> bool` | its text holds the search's, whatever the case or accents | Whether a command is shown, given its text and the search's | +| `className` | `class_` | `string` | | Added after its own classes | + +### Command.Input_group + +| React | solidtype | Type | Default | Description | +|---|---|---|---|---| +| `autoFocus` | `auto_focus` | `bool` | `true` | Whether its input takes the focus as the palette opens | +| `className` | `class_` | `string` | | Added after its own classes | + +### Command.Input_group.Input + +| React | solidtype | Type | Default | Description | +|---|---|---|---|---| +| `placeholder` | `placeholder` | `string` | `"Search commands..."` | Shown while the search is empty | +| `aria-label` | `aria_label` | `string` | `"Search commands"` | Its name | +| `className` | `class_` | `string` | | Added after its own classes | + +### Command.Input_group.Clear_button + +| React | solidtype | Type | Description | +|---|---|---|---| +| `onPress` | `on_click` | `Dom.mouseEvent -> unit` | Called as it is pressed, before it empties the search | +| `className` | `class_` | `string` | Added after its own classes | + +### Command.List + +| React | solidtype | Type | Default | Description | +|---|---|---|---|---| +| `onAction` | `on_action` | `string -> unit` | | Called with a command's key as it runs | +| `disabledKeys` | `disabled_keys` | `string array` | | The keys of the commands disabled | +| `renderEmptyState` | `render_empty_state` | `unit -> element` | | Drawn while no command is shown | +| `className` | `class_` | `string` | | Added after its own classes | + +### Command.Group + +| React | solidtype | Type | Description | +|---|---|---|---| +| `heading` | `heading` | element | Its name | +| `className` | `class_` | `string` | Added after its own classes | + +### Command.Item + +| React | solidtype | Type | Default | Description | +|---|---|---|---|---| +| `id` | `id` | `string` | | Its key | +| `textValue` | `text_value` | `string` | its text | What the search is matched against | +| `isDisabled` | `is_disabled` | `bool` | `false` | Whether it is disabled | +| `onAction` | `on_action` | `unit -> unit` | | Called as it runs | +| `className` | `class_` | `string` | | Added after its own classes | + +### Other parts + +`Command.Header`, `Command.Input_group.Prefix`, `Command.Input_group.Suffix`, +`Command.Separator` and `Command.Footer`. + +| React | solidtype | Type | Description | +|---|---|---|---| +| `className` | `class_` | `string` | Added after its own classes | diff --git a/agents/pages/heroui/coverage.md b/agents/pages/heroui/coverage.md index b2d4463..8788f7c 100644 --- a/agents/pages/heroui/coverage.md +++ b/agents/pages/heroui/coverage.md @@ -97,6 +97,7 @@ imported as [Theming](theming.md#setup) shows. | Component | Status | |---|---| +| [Command](command.md) | made: a search that filters a menu of commands in a dialog, kept or controlled, by its text or a filter given, the focus kept in the search while the keys move through the commands shown, groups, separators, an empty state, a header and a footer, sizes, backdrops | | [Data Grid](data-grid.md) | made: rows as data and columns as definitions, sorted in the browser by text in the page's order or a comparison, or by the application, selection checkboxes, columns resized, pinned to either side and aligned, a header kept in view, rows dragged by a handle and the keys, cells of any control, an empty state, loading more, virtualized, a footer | | [NumberStepper](number-stepper.md) | made: a number field's value, range, step, format and keys, sizes, a value that rolls, a custom value, custom icons and buttons, reversed, disabled | | [Segment](segment.md) | made: one option always selected, controlled or kept, default and ghost, sizes, disabled whole or by option, separators, icons, options that show their label only while selected, an indicator that slides between options | diff --git a/agents/pages/heroui/date-field.md b/agents/pages/heroui/date-field.md index 1149da3..354a536 100644 --- a/agents/pages/heroui/date-field.md +++ b/agents/pages/heroui/date-field.md @@ -274,7 +274,7 @@ let make () = is_disabled=true class_="w-[256px]" name="date" - value=(Js.Nullable.return (D.today (D.get_local_time_zone ())))> + value=(Js.Null.return (D.today (D.get_local_time_zone ())))> @@ -475,7 +475,7 @@ let make () = @@ -541,7 +541,7 @@ let make () = @@ -612,7 +612,7 @@ let make () = is_invalid=(is_invalid ()) min_value=today name="date" - value=(Js.Nullable.fromOption (S.get value)) + value=(Js.Null.fromOption (S.get value)) on_change=(S.set set_value)> @@ -670,7 +670,7 @@ let make () = is_invalid=(is_invalid ()) min_value=today name="date" - value=(Js.Nullable.fromOption (S.get value)) + value=(Js.Null.fromOption (S.get value)) on_change=(S.set set_value)> @@ -770,7 +770,7 @@ Each prop beside the name HeroUI's React component gives it. | React | solidtype | Type | Default | Description | |---|---|---|---|---| -| `value` | `value` | `CalendarDate.t Js.Nullable.t` | | Current value (controlled); null controls it empty | +| `value` | `value` | `CalendarDate.t Js.Null.t` | | Current value (controlled); null controls it empty | | `defaultValue` | `default_value` | `CalendarDate.t` | | Default value (uncontrolled) | | `onChange` | `on_change` | `CalendarDate.t option -> unit` | | Called as soon as its segments make a date other than its value, and with `None` once every segment is erased | | `placeholderValue` | `placeholder_value` | `CalendarDate.t` | today | What an arrow key steps an empty segment from | diff --git a/agents/pages/heroui/date-picker.md b/agents/pages/heroui/date-picker.md index d152a71..3fc10b9 100644 --- a/agents/pages/heroui/date-picker.md +++ b/agents/pages/heroui/date-picker.md @@ -115,7 +115,7 @@ let make () = is_disabled=true class_="w-72" name="date" - value=(Js.Nullable.return (D.today (D.get_local_time_zone ())))> + value=(Js.Null.return (D.today (D.get_local_time_zone ())))> @@ -178,7 +178,7 @@ let make () =
@@ -267,7 +267,7 @@ let make () = is_invalid=(is_invalid ()) min_value=current name="date" - value=(Js.Nullable.fromOption (S.get value)) + value=(Js.Null.fromOption (S.get value)) on_change=(S.set set_value)> @@ -461,8 +461,7 @@ let make () = name="time" placeholder_value=(at_time ()) should_force_leading_zeros=(S.get leading_zeros) - value=(Js.Nullable.fromOption - (S.get picker.state.time_value)) + value=(Js.Null.fromOption (S.get picker.state.time_value)) on_change=(Option.iter picker.state.set_time_value)> @@ -558,7 +557,7 @@ let make () = is_invalid=(is_invalid ()) min_value=current name="appointmentDate" - value=(Js.Nullable.fromOption (S.get value)) + value=(Js.Null.fromOption (S.get value)) on_change=(S.set set_value)> @@ -890,7 +889,7 @@ It takes every prop a [DateField](date-field.md) takes, and: | React | solidtype | Type | Default | Description | |---|---|---|---|---| -| `value` | `value` | `CalendarDate.t Js.Nullable.t` | | Controlled selected date; null controls it empty | +| `value` | `value` | `CalendarDate.t Js.Null.t` | | Controlled selected date; null controls it empty | | `defaultValue` | `default_value` | `CalendarDate.t` | | Default selected value in uncontrolled mode | | `onChange` | `on_change` | `CalendarDate.t option -> unit` | | Called when the selected date changes | | `isOpen` | `is_open` | `bool` | | Controlled popover open state | diff --git a/agents/pages/heroui/date-range-picker.md b/agents/pages/heroui/date-range-picker.md index 441aa3c..5b578ff 100644 --- a/agents/pages/heroui/date-range-picker.md +++ b/agents/pages/heroui/date-range-picker.md @@ -135,7 +135,7 @@ let make () = class_="w-80" end_name="endDate" start_name="startDate" - value=(Js.Nullable.return (from_today 4))> + value=(Js.Null.return (from_today 4))> @@ -207,7 +207,7 @@ let make () = @@ -303,7 +303,7 @@ let make () = start_name="startDate" is_invalid=(is_invalid ()) min_value=current - value=(Js.Nullable.fromOption (S.get value)) + value=(Js.Null.fromOption (S.get value)) on_change=(S.set set_value)> @@ -492,7 +492,7 @@ let make () = name placeholder_value=(at "2026-02-03T08:45:00") should_force_leading_zeros=(S.get leading_zeros) - value=(Js.Nullable.fromOption (value ())) + value=(Js.Null.fromOption (value ())) on_change=(Option.iter set)> @@ -644,7 +644,7 @@ let make () = is_invalid=(is_invalid ()) min_value=current start_name="tripStartDate" - value=(Js.Nullable.fromOption (S.get value)) + value=(Js.Null.fromOption (S.get value)) on_change=(S.set set_value)> @@ -991,7 +991,7 @@ Each prop beside the name HeroUI's React component gives it. | React | solidtype | Type | Default | Description | |---|---|---|---|---| -| `value` | `value` | `CalendarDate.t range Js.Nullable.t` | | Controlled selected range; null controls it empty | +| `value` | `value` | `CalendarDate.t range Js.Null.t` | | Controlled selected range; null controls it empty | | `defaultValue` | `default_value` | `CalendarDate.t range` | | Default selected range in uncontrolled mode | | `onChange` | `on_change` | `CalendarDate.t range option -> unit` | | Called when the selected range changes | | `isOpen` | `is_open` | `bool` | | Controlled popover open state | diff --git a/agents/pages/heroui/modal.md b/agents/pages/heroui/modal.md index 219577f..9855f08 100644 --- a/agents/pages/heroui/modal.md +++ b/agents/pages/heroui/modal.md @@ -219,7 +219,9 @@ let make () = let label () = String.capitalize_ascii (S.get scroll) in
+ orientation=`horizontal + value=(Js.Null.return (S.get scroll)) + on_change=(S.set set_scroll)> "Inside" diff --git a/agents/pages/heroui/radio-group.md b/agents/pages/heroui/radio-group.md index cd92079..ab0289c 100644 --- a/agents/pages/heroui/radio-group.md +++ b/agents/pages/heroui/radio-group.md @@ -212,7 +212,8 @@ module Description = Solidtype_heroui.Description let make () = let value, set_value = S.create_signal "pro" in
- + @@ -461,7 +462,7 @@ string React takes. | React | solidtype | Type | Default | Description | |---|---|---|---|---| -| `value` | `value` | `string` | | The current value (controlled) | +| `value` | `value` | `string Js.Null.t` | | The current value (controlled); null controls it empty | | `defaultValue` | `default_value` | `string` | none | The default value (uncontrolled) | | `onChange` | `on_change` | `string -> unit` | | Handler called when the value changes | | `orientation` | `orientation` | `` [ `horizontal \| `vertical ] `` | `` `vertical `` | The orientation of the radio group | diff --git a/agents/pages/heroui/range-calendar.md b/agents/pages/heroui/range-calendar.md index 5f46003..5dd79c2 100644 --- a/agents/pages/heroui/range-calendar.md +++ b/agents/pages/heroui/range-calendar.md @@ -235,7 +235,7 @@ let make () = aria_label="Trip dates" first_day_of_week=`mon focused_value=(S.get focused) - value=(Js.Nullable.fromOption (S.get value)) + value=(Js.Null.fromOption (S.get value)) on_change=(S.set set_value) on_focus_change=(S.set set_focused)> @@ -744,7 +744,7 @@ let make () = aria_label="Trip dates" first_day_of_week=`mon is_invalid=(too_long ()) - value=(Js.Nullable.return (S.get value)) + value=(Js.Null.return (S.get value)) on_change=(Option.iter (S.set set_value))> @@ -906,7 +906,7 @@ let make () = aria_label="Booking range" is_date_unavailable min_value=(D.today (D.get_local_time_zone ())) - value=(Js.Nullable.fromOption (S.get selected)) + value=(Js.Null.fromOption (S.get selected)) on_change=(S.set set_selected)> @@ -1178,7 +1178,7 @@ Each prop beside the name HeroUI's React component gives it. | React | solidtype | Type | Default | Description | |---|---|---|---|---| -| `value` | `value` | `range Js.Nullable.t` | | Controlled selected range; null controls it empty | +| `value` | `value` | `range Js.Null.t` | | Controlled selected range; null controls it empty | | `defaultValue` | `default_value` | `range` | | Initial selected range (uncontrolled) | | `onChange` | `on_change` | `range option -> unit` | | Called when a range is chosen, or cleared | | `allowsNonContiguousRanges` | `allows_non_contiguous_ranges` | `bool` | `false` | Allows ranges that span unavailable dates | diff --git a/agents/pages/heroui/time-field.md b/agents/pages/heroui/time-field.md index 431215c..b49b5ca 100644 --- a/agents/pages/heroui/time-field.md +++ b/agents/pages/heroui/time-field.md @@ -254,7 +254,7 @@ let make () = is_disabled=true class_="w-[256px]" name="time" - value=(Js.Nullable.return time_value)> + value=(Js.Null.return time_value)> @@ -372,7 +372,7 @@ let make () = @@ -430,7 +430,7 @@ let make () = @@ -503,7 +503,7 @@ let make () = name="time" min_value=min_time max_value=max_time - value=(Js.Nullable.fromOption (S.get value)) + value=(Js.Null.fromOption (S.get value)) on_change=(S.set set_value)> @@ -566,7 +566,7 @@ let make () = name="time" min_value=min_time max_value=max_time - value=(Js.Nullable.fromOption (S.get value)) + value=(Js.Null.fromOption (S.get value)) on_change=(S.set set_value)> @@ -671,7 +671,7 @@ Each prop beside the name HeroUI's React component gives it. | React | solidtype | Type | Default | Description | |---|---|---|---|---| -| `value` | `value` | `Time.t Js.Nullable.t` | | Current value (controlled); null controls it empty | +| `value` | `value` | `Time.t Js.Null.t` | | Current value (controlled); null controls it empty | | `defaultValue` | `default_value` | `Time.t` | | Default value (uncontrolled) | | `onChange` | `on_change` | `Time.t option -> unit` | | Called once every segment has its part and the time is another, and with `None` once every segment is erased | | `placeholderValue` | `placeholder_value` | `Time.t` | midnight | Placeholder time an arrow key steps an empty segment from | diff --git a/agents/pages/index.md b/agents/pages/index.md index ada195e..e2ae216 100644 --- a/agents/pages/index.md +++ b/agents/pages/index.md @@ -70,7 +70,7 @@ GitHub release: "solid-js": "~1.9.17", "vite": "^8.3.0", "vite-plugin-solid": "^2.11.14", - "vite-plugin-solidtype": "https://github.com/hyphatech/solidtype/releases/download/0.13.0/vite-plugin-solidtype-0.13.0.tgz" + "vite-plugin-solidtype": "https://github.com/hyphatech/solidtype/releases/download/0.14.0/vite-plugin-solidtype-0.14.0.tgz" } } ``` diff --git a/agents/pages/kobalte/binding.md b/agents/pages/kobalte/binding.md index 012842c..e45e6da 100644 --- a/agents/pages/kobalte/binding.md +++ b/agents/pages/kobalte/binding.md @@ -89,7 +89,8 @@ mapped, and further: | `string`, `boolean` | `string`, `bool` | | a closed set of string literals, `"horizontal" \| "vertical"` | a polymorphic variant, `` [ `horizontal \| `vertical ] `` | | `number` | `float`, since a slider's value is fractional | -| `T \| null` | `T Js.Nullable.t`: Kobalte reads `undefined` as uncontrolled and `null` as controlled and empty | +| `T \| null`, given | `T Js.Null.t`: Kobalte reads `undefined` as uncontrolled and `null` as controlled and empty, and `Js.Null.t` cannot hold `undefined` | +| `T \| null`, read | `T Js.Nullable.t`, which takes whatever JavaScript gives | | an optional prop, `T \| undefined` | an optional argument, `?name` | | a callback's optional argument | an `option` | | `A \| B` of different JavaScript kinds | `(a, b) Types.Either.t`, built by `Either.left` and `Either.right` | @@ -102,7 +103,7 @@ are Kobalte's building blocks -- menu, popper, dismissable layer, polymorphic -- which its documentation does not offer as components. `Root` and `Root_multiple` show why a type is split rather than widened. A -toggle group's value is one `string`, or nothing, `string Js.Nullable.t`; +toggle group's value is one `string`, or nothing, `string Js.Null.t`; with `multiple`, it is a `string array`. TypeScript says which by overloading on `multiple`, which OCaml cannot. Two modules give each its own exact type. [Using Kobalte](usage.md#toggle-group) shows both. diff --git a/agents/pages/kobalte/usage.md b/agents/pages/kobalte/usage.md index c2b826a..cc092a1 100644 --- a/agents/pages/kobalte/usage.md +++ b/agents/pages/kobalte/usage.md @@ -58,14 +58,14 @@ let item = data-[pressed]:bg-accent data-[pressed]:text-accent-foreground" let make () = - let align, set_align = S.create_signal (Js.Nullable.return "left") in + let align, set_align = S.create_signal (Some "left") in let styles, set_styles = S.create_signal [| "bold" |] in
+ value=(Js.Null.fromOption (S.get align)) + on_change=(fun v -> S.set set_align (Js.Nullable.toOption v))> "Left" "Center" "Right" @@ -80,9 +80,7 @@ let make () = "Italic"

- (let align = - Option.value (Js.Nullable.toOption (S.get align)) ~default:"none" - in + (let align = Option.value (S.get align) ~default:"none" in Jsx.string (align ^ "; " ^ String.concat ", " (Array.to_list (S.get styles))))

@@ -90,8 +88,9 @@ let make () = ``` `ToggleGroup.Root` holds one value, or none: its `value` is a -`string Js.Nullable.t`, since Kobalte reads `undefined` as uncontrolled and -`null` as a controlled group with nothing pressed. `ToggleGroup.Root_multiple` +`string Js.Null.t`, since Kobalte reads `undefined` as uncontrolled and +`null` as a controlled group with nothing pressed. `on_change` tells a +`string Js.Nullable.t`, as Kobalte gives it. `ToggleGroup.Root_multiple` holds several, a `string array`, and takes `multiple=true`. Each has the exact type of its value, where TypeScript has one component whose type turns on `multiple`. diff --git a/agents/pages/start/ocaml-over-typescript.md b/agents/pages/start/ocaml-over-typescript.md index 9aa0d4d..8bfb76a 100644 --- a/agents/pages/start/ocaml-over-typescript.md +++ b/agents/pages/start/ocaml-over-typescript.md @@ -206,17 +206,19 @@ Kobalte reads a `value` of `undefined` as uncontrolled, and `null` as controlled and empty. In TSX, `value={selected()}` compiles where `selected()` may be `undefined`, and the moment it is, the select keeps a value of its own and stops following the page. In OCaml a value Kobalte -reads as `null` is a `Js.Nullable.t`, so an empty selection says so: +reads as `null` is a `Js.Null.t`, which cannot hold `undefined`, so an +empty selection says so: ```ocaml let picker () = + value=(Js.Null.fromOption (S.get selected)) /> ``` -`None` is `null`, still controlled. Handing the value to Kobalte is leaving -the prop out. +`None` is `null`, still controlled. `Js.Nullable.fromOption`, whose `None` +is `undefined`, does not compile there. Handing the value to Kobalte is +leaving the prop out. ### It is Solid underneath diff --git a/docs/astro.config.mjs b/docs/astro.config.mjs index 0ad1ec8..437c782 100644 --- a/docs/astro.config.mjs +++ b/docs/astro.config.mjs @@ -105,7 +105,7 @@ export default defineConfig({ { label: "Forms", items: ["heroui/checkbox", "heroui/checkbox-group", "heroui/description", "heroui/error-message", "heroui/field-error", "heroui/fieldset", "heroui/form", "heroui/input", "heroui/input-group", "heroui/label", "heroui/number-field", "heroui/number-stepper", "heroui/radio-group", "heroui/search-field", "heroui/text-area", "heroui/text-field"] }, { label: "Layout", items: ["heroui/card", "heroui/separator", "heroui/surface"] }, { label: "Media", items: ["heroui/avatar", "heroui/avatar-group"] }, - { label: "Navigation", items: ["heroui/accordion", "heroui/breadcrumbs", "heroui/disclosure", "heroui/disclosure-group", "heroui/link", "heroui/pagination", "heroui/tabs"] }, + { label: "Navigation", items: ["heroui/accordion", "heroui/breadcrumbs", "heroui/command", "heroui/disclosure", "heroui/disclosure-group", "heroui/link", "heroui/pagination", "heroui/tabs"] }, { label: "Overlays", items: ["heroui/alert-dialog", "heroui/drawer", "heroui/modal", "heroui/popover", "heroui/toast", "heroui/tooltip"] }, { label: "Pickers", items: ["heroui/autocomplete", "heroui/combo-box", "heroui/select"] }, { label: "Typography", items: ["heroui/kbd", "heroui/typography"] }, diff --git a/docs/islands/calendar_booking.mlx b/docs/islands/calendar_booking.mlx index da42df6..bc4c0b7 100644 --- a/docs/islands/calendar_booking.mlx +++ b/docs/islands/calendar_booking.mlx @@ -18,7 +18,7 @@ let make () = aria_label="Booking date" is_date_unavailable min_value=(D.today (D.get_local_time_zone ())) - value=(Js.Nullable.fromOption (S.get selected)) + value=(Js.Null.fromOption (S.get selected)) on_change=(fun d -> S.set set_selected (Some d))> diff --git a/docs/islands/calendar_controlled.mlx b/docs/islands/calendar_controlled.mlx index eb8f9a8..385dfd3 100644 --- a/docs/islands/calendar_controlled.mlx +++ b/docs/islands/calendar_controlled.mlx @@ -36,7 +36,7 @@ let make () = S.set set_value (Some d)) on_focus_change=(S.set set_focused)> diff --git a/docs/islands/color_field_channel_editing.mlx b/docs/islands/color_field_channel_editing.mlx index b57f171..efca70e 100644 --- a/docs/islands/color_field_channel_editing.mlx +++ b/docs/islands/color_field_channel_editing.mlx @@ -18,7 +18,7 @@ let make () = class_="w-[100px]" color_space=`hsl name - value=(Js.Nullable.fromOption (S.get color)) + value=(Js.Null.fromOption (S.get color)) on_change=(S.set set_color)> diff --git a/docs/islands/color_field_controlled.mlx b/docs/islands/color_field_controlled.mlx index 7019bfb..a334583 100644 --- a/docs/islands/color_field_controlled.mlx +++ b/docs/islands/color_field_controlled.mlx @@ -20,7 +20,7 @@ let make () = diff --git a/docs/islands/color_field_custom_styles.mlx b/docs/islands/color_field_custom_styles.mlx index 5eb060c..4a61343 100644 --- a/docs/islands/color_field_custom_styles.mlx +++ b/docs/islands/color_field_custom_styles.mlx @@ -18,7 +18,7 @@ let make () = "Applied to buttons, links, and focus rings." diff --git a/docs/islands/color_field_form.mlx b/docs/islands/color_field_form.mlx index aa679c9..61beae4 100644 --- a/docs/islands/color_field_form.mlx +++ b/docs/islands/color_field_form.mlx @@ -43,7 +43,7 @@ let make () = is_required=true class_="w-full" name="brand-color" - value=(Js.Nullable.fromOption (S.get value)) + value=(Js.Null.fromOption (S.get value)) on_change=(S.set set_value)> diff --git a/docs/islands/color_field_usage.mlx b/docs/islands/color_field_usage.mlx index 369f7b3..7f21b6d 100644 --- a/docs/islands/color_field_usage.mlx +++ b/docs/islands/color_field_usage.mlx @@ -18,7 +18,7 @@ let make () = diff --git a/docs/islands/command_backdrop_variants.mlx b/docs/islands/command_backdrop_variants.mlx new file mode 100644 index 0000000..b197c96 --- /dev/null +++ b/docs/islands/command_backdrop_variants.mlx @@ -0,0 +1,82 @@ +module Jsx = Solidtype.Jsx +module For = Solidtype.For +module Button = Solidtype_heroui.Button +module Chip = Solidtype_heroui.Chip +module Command = Solidtype_heroui.Command +module Kbd = Solidtype_heroui.Kbd +module Gravity = Solidtype_gravity_icons + +let variants = + [| (`transparent, "transparent"); (`opaque, "opaque"); (`blur, "blur") |] + +let make () = +
+ + (fun (variant, name) _ -> + + + + + + + "Home" + + + + + + + + + "Esc" + + + + + + "Create new file..." + + + + "Create new folder..." + + + "Assign to me" + + + + + "Preferences" + + + "Change theme" + + + "Keyboard shortcuts" + + + + +
+
+
+ + +
+ "Navigate" +
+
+ + "Select" +
+
+
+
+
+
+
) +
+
diff --git a/docs/islands/command_clean.mlx b/docs/islands/command_clean.mlx new file mode 100644 index 0000000..f9bf841 --- /dev/null +++ b/docs/islands/command_clean.mlx @@ -0,0 +1,78 @@ +module Jsx = Solidtype.Jsx +module For = Solidtype.For +module Button = Solidtype_heroui.Button +module Command = Solidtype_heroui.Command +module Kbd = Solidtype_heroui.Kbd +module Gravity = Solidtype_gravity_icons + +let keys shortcut = +
+ + (fun key _ -> + + (Jsx.string key) + ) + +
+ +let make () = + + + + + + + + "home" + + + + + + + + + + + "Search Projects..." + (keys [| "S"; "P" |]) + + + "Create New Project..." + + + + + + "Search Teams..." + (keys [| {js|⇧|js}; "P" |]) + + + "Create New Team..." + + + + + + "Search Docs..." + (keys [| {js|⇧|js}; "D" |]) + + + "Send Feedback..." + + + "Contact Support" + + + + + + + diff --git a/docs/islands/command_dev_toolbar.mlx b/docs/islands/command_dev_toolbar.mlx new file mode 100644 index 0000000..e897b94 --- /dev/null +++ b/docs/islands/command_dev_toolbar.mlx @@ -0,0 +1,76 @@ +module Jsx = Solidtype.Jsx +module Button = Solidtype_heroui.Button +module Command = Solidtype_heroui.Command +module Kbd = Solidtype_heroui.Kbd +module Gravity = Solidtype_gravity_icons + +let shortcut key = + + (Jsx.string key) + + +let make () = + + + + + + + + + + + + + "Esc" + + + + + + +
+ "View Environment" + + "staging/feat-dashboard-redesign" + +
+
+
+ + + "Feedback" (shortcut "C") + + + "Notifications" (shortcut "N") + + + "Feature Flags..." (shortcut "F") + + + "Share Preview" (shortcut "S") + + + + "Switch Branch..." + (shortcut "B") + + + + + "View Logs" (shortcut "L") + + + "Tracing..." (shortcut "T") + + +
+
+
+
+
diff --git a/docs/islands/command_launcher.mlx b/docs/islands/command_launcher.mlx new file mode 100644 index 0000000..49c9a99 --- /dev/null +++ b/docs/islands/command_launcher.mlx @@ -0,0 +1,83 @@ +module Jsx = Solidtype.Jsx +module For = Solidtype.For +module Button = Solidtype_heroui.Button +module Command = Solidtype_heroui.Command +module Kbd = Solidtype_heroui.Kbd +module Gravity = Solidtype_gravity_icons + +(* An icon on a square of its colour, and what kind of entry it is. *) +let entry ~colour ~icon ~kind name = + +
+ icon +
+ (Jsx.string name) + (Jsx.string kind) +
+ +let hint key = + + (Jsx.string key) + + +let make () = + + + + + + + + + + + + (entry ~colour:"bg-violet-500" ~icon: + ~kind:"Application" "Design Tool") + (entry ~colour:"bg-indigo-500" ~icon: + ~kind:"Application" "Project Tracker") + (entry ~colour:"bg-sky-500" ~icon: + ~kind:"Application" "Team Chat") + (entry ~colour:"bg-red-500" ~icon: + ~kind:"Application" "Video Platform") + (entry ~colour:"bg-amber-500" ~icon: + ~kind:"Application" "Notes") + + + (entry + ~colour:"rounded-md bg-gradient-to-b from-red-400 to-red-600" + ~icon: ~kind:"Command" "Clipboard History") + (entry + ~colour: + "rounded-md bg-gradient-to-b from-emerald-400 to-emerald-600" + ~icon: ~kind:"Command" "Import Extension") + (entry + ~colour: + "rounded-md bg-gradient-to-b from-emerald-400 to-emerald-600" + ~icon: ~kind:"Command" "Manage Extensions") + + + +
+ "Open Application" + (hint {js|↵|js}) +
+
+
+ "Actions" +
(hint {js|⌘|js}) (hint "K")
+
+ + + + + diff --git a/docs/islands/command_minimal.mlx b/docs/islands/command_minimal.mlx new file mode 100644 index 0000000..caeec73 --- /dev/null +++ b/docs/islands/command_minimal.mlx @@ -0,0 +1,64 @@ +module Jsx = Solidtype.Jsx +module For = Solidtype.For +module Button = Solidtype_heroui.Button +module Command = Solidtype_heroui.Command +module Kbd = Solidtype_heroui.Kbd +module Gravity = Solidtype_gravity_icons + +(* A command marked by a bar at its start while it is focused, its keys + beside it. *) +let command ~icon ~keys name = + + icon + (Jsx.string name) +
+ + (fun key _ -> + (Jsx.string key)) + +
+
+ +let make () = + + + + + + +
+ "Issue - FUN-343" +
+
+ + + + + + (command ~icon: ~keys:[| "A" |] "Assign to...") + (command ~icon: ~keys:[| "I" |] "Assign to me") + (command ~icon: ~keys:[| "S" |] + "Change status...") + (command ~icon: ~keys:[| "P" |] + "Change priority...") + (command ~icon: ~keys:[| "L" |] "Change labels...") + (command ~icon: ~keys:[| {js|⇧|js}; "L" |] + "Remove label...") + (command ~icon: ~keys:[| {js|⇧|js}; "D" |] + "Set due date...") + +
+
+
+
diff --git a/docs/islands/command_multiple_search_terms.mlx b/docs/islands/command_multiple_search_terms.mlx new file mode 100644 index 0000000..535e3ea --- /dev/null +++ b/docs/islands/command_multiple_search_terms.mlx @@ -0,0 +1,128 @@ +module S = Solidtype.Solid +module Jsx = Solidtype.Jsx +module For = Solidtype.For +module Button = Solidtype_heroui.Button +module Chip = Solidtype_heroui.Chip +module Close_button = Solidtype_heroui.Close_button +module Command = Solidtype_heroui.Command +module Kbd = Solidtype_heroui.Kbd +module Gravity = Solidtype_gravity_icons + +(* An item is shown while every word typed is in its text, in any order. *) +let every_term text typed = + let text = String.lowercase_ascii text in + String.split_on_char ' ' (String.lowercase_ascii typed) + |> List.filter (fun term -> not (String.equal term "")) + |> List.for_all (fun term -> + let n = String.length term and m = String.length text in + let rec from i = + i + n <= m && (String.equal (String.sub text i n) term || from (i + 1)) + in + from 0) + +let make () = + let scopes, set_scopes = + S.create_signal [| "Projects"; "Tasks"; "People"; "Documents"; "Channels" |] + in + + + + + + + + + + + + + + "K" + + + + +
+ + (fun scope _ -> + + (Jsx.string scope) + + S.update set_scopes + (Js.Array.filter ~f:(fun s -> + not (String.equal s scope)))) + /> + ) + +
+
+ + + + + "Summarize this week's progress" + + + "Create a task for the team" + + + "Draft a project brief" + + + "Schedule a standup meeting" + + + + "Results (4)" + + )> + + "View recent activity" + + + "Open project roadmap" + + + "Browse team directory" + + + "Manage workspace settings" + + + + +
+
+
+ + +
+ "Navigate" +
+
+ "Select" +
+
+ + "Not what you're looking for? Try the " + + +
+
+
+
+
diff --git a/docs/islands/command_sizes.mlx b/docs/islands/command_sizes.mlx new file mode 100644 index 0000000..ffdcde8 --- /dev/null +++ b/docs/islands/command_sizes.mlx @@ -0,0 +1,81 @@ +module Jsx = Solidtype.Jsx +module For = Solidtype.For +module Button = Solidtype_heroui.Button +module Chip = Solidtype_heroui.Chip +module Command = Solidtype_heroui.Command +module Kbd = Solidtype_heroui.Kbd +module Gravity = Solidtype_gravity_icons + +let sizes = [| (`sm, "sm"); (`md, "md"); (`lg, "lg") |] + +let make () = +
+ + (fun (size, name) _ -> + + + + + + + "Home" + + + + + + + + + "Esc" + + + + + + "Create new file..." + + + + "Create new folder..." + + + "Assign to me" + + + + + "Preferences" + + + "Change theme" + + + "Keyboard shortcuts" + + + + +
+
+
+ + +
+ "Navigate" +
+
+ + "Select" +
+
+
+
+
+
+
) +
+
diff --git a/docs/islands/command_split_view.mlx b/docs/islands/command_split_view.mlx new file mode 100644 index 0000000..546715d --- /dev/null +++ b/docs/islands/command_split_view.mlx @@ -0,0 +1,122 @@ +module S = Solidtype.Solid +module Jsx = Solidtype.Jsx +module For = Solidtype.For +module Avatar = Solidtype_heroui.Avatar +module Button = Solidtype_heroui.Button +module Chip = Solidtype_heroui.Chip +module Command = Solidtype_heroui.Command +module Input = Solidtype_heroui.Input +module Kbd = Solidtype_heroui.Kbd +module Label = Solidtype_heroui.Label +module Radio = Solidtype_heroui.Radio +module Radio_group = Solidtype_heroui.Radio_group +module Slider = Solidtype_heroui.Slider +module Switch = Solidtype_heroui.Switch +module Gravity = Solidtype_gravity_icons + +let components = + [| + ("Button", "Trigger actions", fun () -> ); + ("Input", "Retrieve user input", fun () -> ); + ("Radio", "Single choice input", fun () -> ); + ("Chip", "Annotate context", fun () -> ); + ("Slider", "Free range picker", fun () -> ); + ("Avatar", "Illustrate the user", fun () -> ); + ("Switch", "Toggle a setting", fun () -> ); + |] + +(* The component the focused item names, drawn beside the list. *) +module Preview = struct + let%component make () = + let palette = Command.Context.use () in +
+ (match S.get palette.focused_key with + | Some "Input" -> + + | Some "Radio" -> + + + + "Basic" + + + + + "Premium" + + + + | Some "Chip" -> "Chip" + | Some "Slider" -> + + + + + | Some "Avatar" -> + "JD" + | Some "Switch" -> + + + + + + + | Some _ | None -> ) +
+end + +let make () = + + + + + + + + + + + + +
+ + + + (fun (name, purpose, icon) _ -> + +
+ (icon ()) +
+
+ + (Jsx.string name) + + + (Jsx.string purpose) + +
+
) +
+
+
+
+ +
+ + + + diff --git a/docs/islands/command_usage.mlx b/docs/islands/command_usage.mlx new file mode 100644 index 0000000..44075f3 --- /dev/null +++ b/docs/islands/command_usage.mlx @@ -0,0 +1,72 @@ +module Jsx = Solidtype.Jsx +module Button = Solidtype_heroui.Button +module Chip = Solidtype_heroui.Chip +module Command = Solidtype_heroui.Command +module Kbd = Solidtype_heroui.Kbd +module Gravity = Solidtype_gravity_icons + +let make () = + + + + + + + "Home" + + + + + + + + + "Esc" + + + + + + "Create new file..." + + + "Create new folder..." + + + "Assign to me" + + + + + "Preferences" + + + "Change theme" + + + "Keyboard shortcuts" + + + + +
+
+
+ + +
+ "Navigate" +
+
+ "Select" +
+
+
+
+
+
+
diff --git a/docs/islands/date_field_controlled.mlx b/docs/islands/date_field_controlled.mlx index 2a6c241..cc82d13 100644 --- a/docs/islands/date_field_controlled.mlx +++ b/docs/islands/date_field_controlled.mlx @@ -12,7 +12,7 @@ let make () = diff --git a/docs/islands/date_field_disabled.mlx b/docs/islands/date_field_disabled.mlx index 9bf78a1..923818b 100644 --- a/docs/islands/date_field_disabled.mlx +++ b/docs/islands/date_field_disabled.mlx @@ -10,7 +10,7 @@ let make () = is_disabled=true class_="w-[256px]" name="date" - value=(Js.Nullable.return (D.today (D.get_local_time_zone ())))> + value=(Js.Null.return (D.today (D.get_local_time_zone ())))> diff --git a/docs/islands/date_field_form.mlx b/docs/islands/date_field_form.mlx index 47c919a..65f7e05 100644 --- a/docs/islands/date_field_form.mlx +++ b/docs/islands/date_field_form.mlx @@ -43,7 +43,7 @@ let make () = is_invalid=(is_invalid ()) min_value=today name="date" - value=(Js.Nullable.fromOption (S.get value)) + value=(Js.Null.fromOption (S.get value)) on_change=(S.set set_value)> diff --git a/docs/islands/date_field_with_validation.mlx b/docs/islands/date_field_with_validation.mlx index c482ffd..975ff6e 100644 --- a/docs/islands/date_field_with_validation.mlx +++ b/docs/islands/date_field_with_validation.mlx @@ -21,7 +21,7 @@ let make () = is_invalid=(is_invalid ()) min_value=today name="date" - value=(Js.Nullable.fromOption (S.get value)) + value=(Js.Null.fromOption (S.get value)) on_change=(S.set set_value)> diff --git a/docs/islands/date_field_zoned.mlx b/docs/islands/date_field_zoned.mlx index 5eb6c7d..b107bf9 100644 --- a/docs/islands/date_field_zoned.mlx +++ b/docs/islands/date_field_zoned.mlx @@ -24,7 +24,7 @@ let make () = diff --git a/docs/islands/date_picker_controlled.mlx b/docs/islands/date_picker_controlled.mlx index 203e435..7985794 100644 --- a/docs/islands/date_picker_controlled.mlx +++ b/docs/islands/date_picker_controlled.mlx @@ -16,7 +16,7 @@ let make () =
diff --git a/docs/islands/date_picker_disabled.mlx b/docs/islands/date_picker_disabled.mlx index 8e2e283..40069b4 100644 --- a/docs/islands/date_picker_disabled.mlx +++ b/docs/islands/date_picker_disabled.mlx @@ -13,7 +13,7 @@ let make () = is_disabled=true class_="w-72" name="date" - value=(Js.Nullable.return (D.today (D.get_local_time_zone ())))> + value=(Js.Null.return (D.today (D.get_local_time_zone ())))> diff --git a/docs/islands/date_picker_form.mlx b/docs/islands/date_picker_form.mlx index dbfdbb7..100dfc3 100644 --- a/docs/islands/date_picker_form.mlx +++ b/docs/islands/date_picker_form.mlx @@ -39,7 +39,7 @@ let make () = is_invalid=(is_invalid ()) min_value=current name="appointmentDate" - value=(Js.Nullable.fromOption (S.get value)) + value=(Js.Null.fromOption (S.get value)) on_change=(S.set set_value)> diff --git a/docs/islands/date_picker_format_options.mlx b/docs/islands/date_picker_format_options.mlx index 4b98eb6..8b15f63 100644 --- a/docs/islands/date_picker_format_options.mlx +++ b/docs/islands/date_picker_format_options.mlx @@ -141,8 +141,7 @@ let make () = name="time" placeholder_value=(at_time ()) should_force_leading_zeros=(S.get leading_zeros) - value=(Js.Nullable.fromOption - (S.get picker.state.time_value)) + value=(Js.Null.fromOption (S.get picker.state.time_value)) on_change=(Option.iter picker.state.set_time_value)> diff --git a/docs/islands/date_picker_validation.mlx b/docs/islands/date_picker_validation.mlx index 5f37323..2c32f47 100644 --- a/docs/islands/date_picker_validation.mlx +++ b/docs/islands/date_picker_validation.mlx @@ -22,7 +22,7 @@ let make () = is_invalid=(is_invalid ()) min_value=current name="date" - value=(Js.Nullable.fromOption (S.get value)) + value=(Js.Null.fromOption (S.get value)) on_change=(S.set set_value)> diff --git a/docs/islands/date_range_picker_controlled.mlx b/docs/islands/date_range_picker_controlled.mlx index 05258f8..5c6e54f 100644 --- a/docs/islands/date_range_picker_controlled.mlx +++ b/docs/islands/date_range_picker_controlled.mlx @@ -21,7 +21,7 @@ let make () = diff --git a/docs/islands/date_range_picker_disabled.mlx b/docs/islands/date_range_picker_disabled.mlx index 51c06ee..d4b9998 100644 --- a/docs/islands/date_range_picker_disabled.mlx +++ b/docs/islands/date_range_picker_disabled.mlx @@ -20,7 +20,7 @@ let make () = class_="w-80" end_name="endDate" start_name="startDate" - value=(Js.Nullable.return (from_today 4))> + value=(Js.Null.return (from_today 4))> diff --git a/docs/islands/date_range_picker_form.mlx b/docs/islands/date_range_picker_form.mlx index 269a3a6..7726ece 100644 --- a/docs/islands/date_range_picker_form.mlx +++ b/docs/islands/date_range_picker_form.mlx @@ -41,7 +41,7 @@ let make () = is_invalid=(is_invalid ()) min_value=current start_name="tripStartDate" - value=(Js.Nullable.fromOption (S.get value)) + value=(Js.Null.fromOption (S.get value)) on_change=(S.set set_value)> diff --git a/docs/islands/date_range_picker_format_options.mlx b/docs/islands/date_range_picker_format_options.mlx index ac33d65..6d62370 100644 --- a/docs/islands/date_range_picker_format_options.mlx +++ b/docs/islands/date_range_picker_format_options.mlx @@ -132,7 +132,7 @@ let make () = name placeholder_value=(at "2026-02-03T08:45:00") should_force_leading_zeros=(S.get leading_zeros) - value=(Js.Nullable.fromOption (value ())) + value=(Js.Null.fromOption (value ())) on_change=(Option.iter set)> diff --git a/docs/islands/date_range_picker_validation.mlx b/docs/islands/date_range_picker_validation.mlx index 2ac6981..409f67e 100644 --- a/docs/islands/date_range_picker_validation.mlx +++ b/docs/islands/date_range_picker_validation.mlx @@ -24,7 +24,7 @@ let make () = start_name="startDate" is_invalid=(is_invalid ()) min_value=current - value=(Js.Nullable.fromOption (S.get value)) + value=(Js.Null.fromOption (S.get value)) on_change=(S.set set_value)> diff --git a/docs/islands/kobalte_toggle_group.mlx b/docs/islands/kobalte_toggle_group.mlx index 7cded90..5bfd4d0 100644 --- a/docs/islands/kobalte_toggle_group.mlx +++ b/docs/islands/kobalte_toggle_group.mlx @@ -7,14 +7,14 @@ let item = data-[pressed]:bg-accent data-[pressed]:text-accent-foreground" let make () = - let align, set_align = S.create_signal (Js.Nullable.return "left") in + let align, set_align = S.create_signal (Some "left") in let styles, set_styles = S.create_signal [| "bold" |] in
+ value=(Js.Null.fromOption (S.get align)) + on_change=(fun v -> S.set set_align (Js.Nullable.toOption v))> "Left" "Center" "Right" @@ -29,9 +29,7 @@ let make () = "Italic"

- (let align = - Option.value (Js.Nullable.toOption (S.get align)) ~default:"none" - in + (let align = Option.value (S.get align) ~default:"none" in Jsx.string (align ^ "; " ^ String.concat ", " (Array.to_list (S.get styles))))

diff --git a/docs/islands/modal_scroll.mlx b/docs/islands/modal_scroll.mlx index 539922f..b6959f7 100644 --- a/docs/islands/modal_scroll.mlx +++ b/docs/islands/modal_scroll.mlx @@ -13,7 +13,9 @@ let make () = let label () = String.capitalize_ascii (S.get scroll) in
+ orientation=`horizontal + value=(Js.Null.return (S.get scroll)) + on_change=(S.set set_scroll)> "Inside" diff --git a/docs/islands/radio_group_controlled.mlx b/docs/islands/radio_group_controlled.mlx index 430eeb6..14647e1 100644 --- a/docs/islands/radio_group_controlled.mlx +++ b/docs/islands/radio_group_controlled.mlx @@ -8,7 +8,8 @@ module Description = Solidtype_heroui.Description let make () = let value, set_value = S.create_signal "pro" in
- + diff --git a/docs/islands/range_calendar_booking.mlx b/docs/islands/range_calendar_booking.mlx index 0d13ad8..857df23 100644 --- a/docs/islands/range_calendar_booking.mlx +++ b/docs/islands/range_calendar_booking.mlx @@ -18,7 +18,7 @@ let make () = aria_label="Booking range" is_date_unavailable min_value=(D.today (D.get_local_time_zone ())) - value=(Js.Nullable.fromOption (S.get selected)) + value=(Js.Null.fromOption (S.get selected)) on_change=(S.set set_selected)> diff --git a/docs/islands/range_calendar_controlled.mlx b/docs/islands/range_calendar_controlled.mlx index c8b1907..30368c5 100644 --- a/docs/islands/range_calendar_controlled.mlx +++ b/docs/islands/range_calendar_controlled.mlx @@ -49,7 +49,7 @@ let make () = aria_label="Trip dates" first_day_of_week=`mon focused_value=(S.get focused) - value=(Js.Nullable.fromOption (S.get value)) + value=(Js.Null.fromOption (S.get value)) on_change=(S.set set_value) on_focus_change=(S.set set_focused)> diff --git a/docs/islands/range_calendar_invalid.mlx b/docs/islands/range_calendar_invalid.mlx index d923531..d655b08 100644 --- a/docs/islands/range_calendar_invalid.mlx +++ b/docs/islands/range_calendar_invalid.mlx @@ -20,7 +20,7 @@ let make () = aria_label="Trip dates" first_day_of_week=`mon is_invalid=(too_long ()) - value=(Js.Nullable.return (S.get value)) + value=(Js.Null.return (S.get value)) on_change=(Option.iter (S.set set_value))> diff --git a/docs/islands/time_field_controlled.mlx b/docs/islands/time_field_controlled.mlx index cdc11b1..00f739a 100644 --- a/docs/islands/time_field_controlled.mlx +++ b/docs/islands/time_field_controlled.mlx @@ -22,7 +22,7 @@ let make () = diff --git a/docs/islands/time_field_disabled.mlx b/docs/islands/time_field_disabled.mlx index 8443900..9fa9efe 100644 --- a/docs/islands/time_field_disabled.mlx +++ b/docs/islands/time_field_disabled.mlx @@ -21,7 +21,7 @@ let make () = is_disabled=true class_="w-[256px]" name="time" - value=(Js.Nullable.return time_value)> + value=(Js.Null.return time_value)> diff --git a/docs/islands/time_field_form.mlx b/docs/islands/time_field_form.mlx index f851b29..ae01766 100644 --- a/docs/islands/time_field_form.mlx +++ b/docs/islands/time_field_form.mlx @@ -47,7 +47,7 @@ let make () = name="time" min_value=min_time max_value=max_time - value=(Js.Nullable.fromOption (S.get value)) + value=(Js.Null.fromOption (S.get value)) on_change=(S.set set_value)> diff --git a/docs/islands/time_field_with_validation.mlx b/docs/islands/time_field_with_validation.mlx index a70c335..731249c 100644 --- a/docs/islands/time_field_with_validation.mlx +++ b/docs/islands/time_field_with_validation.mlx @@ -25,7 +25,7 @@ let make () = name="time" min_value=min_time max_value=max_time - value=(Js.Nullable.fromOption (S.get value)) + value=(Js.Null.fromOption (S.get value)) on_change=(S.set set_value)> diff --git a/docs/islands/time_field_zoned.mlx b/docs/islands/time_field_zoned.mlx index a111a93..75b2124 100644 --- a/docs/islands/time_field_zoned.mlx +++ b/docs/islands/time_field_zoned.mlx @@ -17,7 +17,7 @@ let make () = diff --git a/docs/src/content/docs/heroui/calendar.mdx b/docs/src/content/docs/heroui/calendar.mdx index 2645a1e..a2ab87e 100644 --- a/docs/src/content/docs/heroui/calendar.mdx +++ b/docs/src/content/docs/heroui/calendar.mdx @@ -247,7 +247,7 @@ Each prop beside the name HeroUI's React component gives it. | React | solidtype | Type | Default | Description | |---|---|---|---|---| -| `value` | `value` | `CalendarDate.t Js.Nullable.t` | | Controlled selected date; null controls it empty | +| `value` | `value` | `CalendarDate.t Js.Null.t` | | Controlled selected date; null controls it empty | | `defaultValue` | `default_value` | `CalendarDate.t` | | Initial selected date (uncontrolled) | | `onChange` | `on_change` | `CalendarDate.t -> unit` | | Called when a date is chosen | | `selectionMode` | — | | | `Calendar.Multiple` chooses several dates | diff --git a/docs/src/content/docs/heroui/color-field.mdx b/docs/src/content/docs/heroui/color-field.mdx index 17b6d87..7f7aceb 100644 --- a/docs/src/content/docs/heroui/color-field.mdx +++ b/docs/src/content/docs/heroui/color-field.mdx @@ -200,7 +200,7 @@ Each prop beside the name HeroUI's React component gives it. | React | solidtype | Type | Default | Description | |---|---|---|---|---| -| `value` | `value` | `Types.Color.t Js.Nullable.t` | | Current value (controlled), null for none | +| `value` | `value` | `Types.Color.t Js.Null.t` | | Current value (controlled), null for none | | `defaultValue` | `default_value` | `Types.Color.t` | | Default value (uncontrolled) | | `onChange` | `on_change` | `Types.Color.t option -> unit` | | Handler called when the value changes, `None` for none | | `channel` | `channel` | `Color_field.channel` | | The color channel to edit, fixed as it is made. If not provided, edits hex value | diff --git a/docs/src/content/docs/heroui/command.mdx b/docs/src/content/docs/heroui/command.mdx new file mode 100644 index 0000000..7419876 --- /dev/null +++ b/docs/src/content/docs/heroui/command.mdx @@ -0,0 +1,238 @@ +--- +title: Command +description: A command palette, a search that filters a menu of commands in a dialog. +--- + +import Example from "@components/Example.astro"; +import * as Usage from "@islands/command_usage.jsx"; +import UsageCode from "@islands/command_usage.mlx?raw"; +import * as Clean from "@islands/command_clean.jsx"; +import CleanCode from "@islands/command_clean.mlx?raw"; +import * as DevToolbar from "@islands/command_dev_toolbar.jsx"; +import DevToolbarCode from "@islands/command_dev_toolbar.mlx?raw"; +import * as Launcher from "@islands/command_launcher.jsx"; +import LauncherCode from "@islands/command_launcher.mlx?raw"; +import * as Minimal from "@islands/command_minimal.jsx"; +import MinimalCode from "@islands/command_minimal.mlx?raw"; +import * as SplitView from "@islands/command_split_view.jsx"; +import SplitViewCode from "@islands/command_split_view.mlx?raw"; +import * as MultipleSearchTerms from "@islands/command_multiple_search_terms.jsx"; +import MultipleSearchTermsCode from "@islands/command_multiple_search_terms.mlx?raw"; +import * as Sizes from "@islands/command_sizes.jsx"; +import SizesCode from "@islands/command_sizes.mlx?raw"; +import * as BackdropVariants from "@islands/command_backdrop_variants.jsx"; +import BackdropVariantsCode from "@islands/command_backdrop_variants.mlx?raw"; + +A command palette: a search that filters a menu of commands, in a dialog. The +focus stays in the search while the arrow keys move through the commands +shown; Enter or a press runs one. Its look is a stylesheet the library ships, +imported as [Theming](/solidtype/heroui/theming/#setup) shows. + +## Usage + + + +## Anatomy + +```ocaml + + + + + + + + + + + + + + + + + + + + + + + +``` + +A `Button` inside `Command` opens it. Typing shows the commands whose text +holds what is typed, and focuses the first. Escape empties the search, then +closes the palette. The palette stays open as a command runs: close it in +`on_action` where it should. + +## Examples + +### Clean + + + +### Dev Toolbar + + + +### Launcher + + + +### Minimal + + + +### Split View + +`Command.Context.use ()`, called in a component inside the dialog, gives the +focused command's key, read with `S.get`: here it picks the preview. + + + +### Multiple Search Terms + +`filter` decides which commands are shown, given a command's text and the +search's: here every word typed must be in it. + + + +### Sizes + + + +### Backdrop Variants + + + +## Styling Reference + +### CSS Classes + +- `.command__backdrop`, and its variants `.command__backdrop--transparent`, + `.command__backdrop--opaque`, `.command__backdrop--blur` +- `.command__container` +- `.command__dialog`, and its sizes `.command__dialog--sm`, + `.command__dialog--md`, `.command__dialog--lg` +- `.command__header` +- `.command__input-group`, `.command__input-group-prefix`, + `.command__input-group-input`, `.command__input-group-clear-button`, + `.command__input-group-suffix` +- `.command__list`, `.command__empty` +- `.command__group`, `.command__group-heading` +- `.command__item` +- `.command__separator` +- `.command__footer` + +### Interactive States + +- **Entering**, **Exiting**: `[data-entering="true"]`, `[data-exiting="true"]` + on `.command__backdrop` and `.command__container` +- **Focused**: `[data-focused="true"]` on `.command__item`, the command the keys + or the pointer reached +- **Hovered**: `[data-hovered="true"]` on `.command__item` +- **Pressed**: `[data-pressed="true"]` on `.command__item` +- **Disabled**: `[data-disabled="true"]` on `.command__item` +- **Empty**: `[data-empty="true"]` on `.command__input-group` while the search + is, which hides its clear button + +## API Reference + +Each prop beside its name in React's convention. + +### Command + +| React | solidtype | Type | Default | Description | +|---|---|---|---|---| +| `isOpen` | `is_open` | `bool` | | Whether it is open (controlled) | +| `defaultOpen` | `default_open` | `bool` | | Whether it is open at first (uncontrolled) | +| `onOpenChange` | `on_open_change` | `bool -> unit` | | Called as it opens and closes | +| `children` | `children` | elements | | Its trigger and its backdrop | + +### Command.Backdrop + +| React | solidtype | Type | Default | Description | +|---|---|---|---|---| +| `variant` | `variant` | `` [ `opaque \| `blur \| `transparent ] `` | `` `opaque `` | How it covers the page | +| `isDismissable` | `is_dismissable` | `bool` | `true` | Whether a press on it closes the palette | +| `isKeyboardDismissDisabled` | `is_keyboard_dismiss_disabled` | `bool` | `false` | Whether Escape in an empty search leaves it open | +| `isOpen` | `is_open` | `bool` | | Whether it is open, with no `Command` around it | +| `defaultOpen` | `default_open` | `bool` | | Whether it is open at first, with no `Command` around it | +| `onOpenChange` | `on_open_change` | `bool -> unit` | | Called as it opens and closes, with no `Command` around it | +| `UNSTABLE_portalContainer` | `portal_container` | element | | Where it is drawn, else the page's body | +| `className` | `class_` | `string` | | Added after its own classes | + +### Command.Container + +| React | solidtype | Type | Default | Description | +|---|---|---|---|---| +| `size` | `size` | `` [ `sm \| `md \| `lg ] `` | `` `md `` | The palette's width and greatest height | +| `className` | `class_` | `string` | | Added after its own classes | + +### Command.Dialog + +| React | solidtype | Type | Default | Description | +|---|---|---|---|---| +| `aria-label` | `aria_label` | `string` | `"Command palette"` | Its name | +| `inputValue` | `input_value` | `string` | | The search's text (controlled) | +| `defaultInputValue` | `default_input_value` | `string` | | The search's text at first (uncontrolled) | +| `onInputChange` | `on_input_change` | `string -> unit` | | Called with the search's text as it changes | +| `filter` | `filter` | `string -> string -> bool` | its text holds the search's, whatever the case or accents | Whether a command is shown, given its text and the search's | +| `className` | `class_` | `string` | | Added after its own classes | + +### Command.Input_group + +| React | solidtype | Type | Default | Description | +|---|---|---|---|---| +| `autoFocus` | `auto_focus` | `bool` | `true` | Whether its input takes the focus as the palette opens | +| `className` | `class_` | `string` | | Added after its own classes | + +### Command.Input_group.Input + +| React | solidtype | Type | Default | Description | +|---|---|---|---|---| +| `placeholder` | `placeholder` | `string` | `"Search commands..."` | Shown while the search is empty | +| `aria-label` | `aria_label` | `string` | `"Search commands"` | Its name | +| `className` | `class_` | `string` | | Added after its own classes | + +### Command.Input_group.Clear_button + +| React | solidtype | Type | Description | +|---|---|---|---| +| `onPress` | `on_click` | `Dom.mouseEvent -> unit` | Called as it is pressed, before it empties the search | +| `className` | `class_` | `string` | Added after its own classes | + +### Command.List + +| React | solidtype | Type | Default | Description | +|---|---|---|---|---| +| `onAction` | `on_action` | `string -> unit` | | Called with a command's key as it runs | +| `disabledKeys` | `disabled_keys` | `string array` | | The keys of the commands disabled | +| `renderEmptyState` | `render_empty_state` | `unit -> element` | | Drawn while no command is shown | +| `className` | `class_` | `string` | | Added after its own classes | + +### Command.Group + +| React | solidtype | Type | Description | +|---|---|---|---| +| `heading` | `heading` | element | Its name | +| `className` | `class_` | `string` | Added after its own classes | + +### Command.Item + +| React | solidtype | Type | Default | Description | +|---|---|---|---|---| +| `id` | `id` | `string` | | Its key | +| `textValue` | `text_value` | `string` | its text | What the search is matched against | +| `isDisabled` | `is_disabled` | `bool` | `false` | Whether it is disabled | +| `onAction` | `on_action` | `unit -> unit` | | Called as it runs | +| `className` | `class_` | `string` | | Added after its own classes | + +### Other parts + +`Command.Header`, `Command.Input_group.Prefix`, `Command.Input_group.Suffix`, +`Command.Separator` and `Command.Footer`. + +| React | solidtype | Type | Description | +|---|---|---|---| +| `className` | `class_` | `string` | Added after its own classes | diff --git a/docs/src/content/docs/heroui/coverage.mdx b/docs/src/content/docs/heroui/coverage.mdx index 52f7675..0d31bb0 100644 --- a/docs/src/content/docs/heroui/coverage.mdx +++ b/docs/src/content/docs/heroui/coverage.mdx @@ -98,6 +98,7 @@ imported as [Theming](/solidtype/heroui/theming/#setup) shows. | Component | Status | |---|---| +| [Command](/solidtype/heroui/command/) | made: a search that filters a menu of commands in a dialog, kept or controlled, by its text or a filter given, the focus kept in the search while the keys move through the commands shown, groups, separators, an empty state, a header and a footer, sizes, backdrops | | [Data Grid](/solidtype/heroui/data-grid/) | made: rows as data and columns as definitions, sorted in the browser by text in the page's order or a comparison, or by the application, selection checkboxes, columns resized, pinned to either side and aligned, a header kept in view, rows dragged by a handle and the keys, cells of any control, an empty state, loading more, virtualized, a footer | | [NumberStepper](/solidtype/heroui/number-stepper/) | made: a number field's value, range, step, format and keys, sizes, a value that rolls, a custom value, custom icons and buttons, reversed, disabled | | [Segment](/solidtype/heroui/segment/) | made: one option always selected, controlled or kept, default and ghost, sizes, disabled whole or by option, separators, icons, options that show their label only while selected, an indicator that slides between options | diff --git a/docs/src/content/docs/heroui/date-field.mdx b/docs/src/content/docs/heroui/date-field.mdx index ad52b9b..a6f4d32 100644 --- a/docs/src/content/docs/heroui/date-field.mdx +++ b/docs/src/content/docs/heroui/date-field.mdx @@ -238,7 +238,7 @@ Each prop beside the name HeroUI's React component gives it. | React | solidtype | Type | Default | Description | |---|---|---|---|---| -| `value` | `value` | `CalendarDate.t Js.Nullable.t` | | Current value (controlled); null controls it empty | +| `value` | `value` | `CalendarDate.t Js.Null.t` | | Current value (controlled); null controls it empty | | `defaultValue` | `default_value` | `CalendarDate.t` | | Default value (uncontrolled) | | `onChange` | `on_change` | `CalendarDate.t option -> unit` | | Called as soon as its segments make a date other than its value, and with `None` once every segment is erased | | `placeholderValue` | `placeholder_value` | `CalendarDate.t` | today | What an arrow key steps an empty segment from | diff --git a/docs/src/content/docs/heroui/date-picker.mdx b/docs/src/content/docs/heroui/date-picker.mdx index 6e2a2e5..b978fd8 100644 --- a/docs/src/content/docs/heroui/date-picker.mdx +++ b/docs/src/content/docs/heroui/date-picker.mdx @@ -156,7 +156,7 @@ It takes every prop a [DateField](/solidtype/heroui/date-field/) takes, and: | React | solidtype | Type | Default | Description | |---|---|---|---|---| -| `value` | `value` | `CalendarDate.t Js.Nullable.t` | | Controlled selected date; null controls it empty | +| `value` | `value` | `CalendarDate.t Js.Null.t` | | Controlled selected date; null controls it empty | | `defaultValue` | `default_value` | `CalendarDate.t` | | Default selected value in uncontrolled mode | | `onChange` | `on_change` | `CalendarDate.t option -> unit` | | Called when the selected date changes | | `isOpen` | `is_open` | `bool` | | Controlled popover open state | diff --git a/docs/src/content/docs/heroui/date-range-picker.mdx b/docs/src/content/docs/heroui/date-range-picker.mdx index 98d9106..ac9f309 100644 --- a/docs/src/content/docs/heroui/date-range-picker.mdx +++ b/docs/src/content/docs/heroui/date-range-picker.mdx @@ -164,7 +164,7 @@ Each prop beside the name HeroUI's React component gives it. | React | solidtype | Type | Default | Description | |---|---|---|---|---| -| `value` | `value` | `CalendarDate.t range Js.Nullable.t` | | Controlled selected range; null controls it empty | +| `value` | `value` | `CalendarDate.t range Js.Null.t` | | Controlled selected range; null controls it empty | | `defaultValue` | `default_value` | `CalendarDate.t range` | | Default selected range in uncontrolled mode | | `onChange` | `on_change` | `CalendarDate.t range option -> unit` | | Called when the selected range changes | | `isOpen` | `is_open` | `bool` | | Controlled popover open state | diff --git a/docs/src/content/docs/heroui/radio-group.mdx b/docs/src/content/docs/heroui/radio-group.mdx index 6a14fcb..2ad16fd 100644 --- a/docs/src/content/docs/heroui/radio-group.mdx +++ b/docs/src/content/docs/heroui/radio-group.mdx @@ -180,7 +180,7 @@ string React takes. | React | solidtype | Type | Default | Description | |---|---|---|---|---| -| `value` | `value` | `string` | | The current value (controlled) | +| `value` | `value` | `string Js.Null.t` | | The current value (controlled); null controls it empty | | `defaultValue` | `default_value` | `string` | none | The default value (uncontrolled) | | `onChange` | `on_change` | `string -> unit` | | Handler called when the value changes | | `orientation` | `orientation` | `` [ `horizontal \| `vertical ] `` | `` `vertical `` | The orientation of the radio group | diff --git a/docs/src/content/docs/heroui/range-calendar.mdx b/docs/src/content/docs/heroui/range-calendar.mdx index 753003e..c8d356e 100644 --- a/docs/src/content/docs/heroui/range-calendar.mdx +++ b/docs/src/content/docs/heroui/range-calendar.mdx @@ -259,7 +259,7 @@ Each prop beside the name HeroUI's React component gives it. | React | solidtype | Type | Default | Description | |---|---|---|---|---| -| `value` | `value` | `range Js.Nullable.t` | | Controlled selected range; null controls it empty | +| `value` | `value` | `range Js.Null.t` | | Controlled selected range; null controls it empty | | `defaultValue` | `default_value` | `range` | | Initial selected range (uncontrolled) | | `onChange` | `on_change` | `range option -> unit` | | Called when a range is chosen, or cleared | | `allowsNonContiguousRanges` | `allows_non_contiguous_ranges` | `bool` | `false` | Allows ranges that span unavailable dates | diff --git a/docs/src/content/docs/heroui/time-field.mdx b/docs/src/content/docs/heroui/time-field.mdx index de2336e..5697a71 100644 --- a/docs/src/content/docs/heroui/time-field.mdx +++ b/docs/src/content/docs/heroui/time-field.mdx @@ -218,7 +218,7 @@ Each prop beside the name HeroUI's React component gives it. | React | solidtype | Type | Default | Description | |---|---|---|---|---| -| `value` | `value` | `Time.t Js.Nullable.t` | | Current value (controlled); null controls it empty | +| `value` | `value` | `Time.t Js.Null.t` | | Current value (controlled); null controls it empty | | `defaultValue` | `default_value` | `Time.t` | | Default value (uncontrolled) | | `onChange` | `on_change` | `Time.t option -> unit` | | Called once every segment has its part and the time is another, and with `None` once every segment is erased | | `placeholderValue` | `placeholder_value` | `Time.t` | midnight | Placeholder time an arrow key steps an empty segment from | diff --git a/docs/src/content/docs/index.mdx b/docs/src/content/docs/index.mdx index 3292fbe..2182cc0 100644 --- a/docs/src/content/docs/index.mdx +++ b/docs/src/content/docs/index.mdx @@ -65,7 +65,7 @@ GitHub release: "solid-js": "~1.9.17", "vite": "^8.3.0", "vite-plugin-solid": "^2.11.14", - "vite-plugin-solidtype": "https://github.com/hyphatech/solidtype/releases/download/0.13.0/vite-plugin-solidtype-0.13.0.tgz" + "vite-plugin-solidtype": "https://github.com/hyphatech/solidtype/releases/download/0.14.0/vite-plugin-solidtype-0.14.0.tgz" } } ``` diff --git a/docs/src/content/docs/kobalte/binding.mdx b/docs/src/content/docs/kobalte/binding.mdx index ecebcb7..e2593f1 100644 --- a/docs/src/content/docs/kobalte/binding.mdx +++ b/docs/src/content/docs/kobalte/binding.mdx @@ -90,7 +90,8 @@ mapped, and further: | `string`, `boolean` | `string`, `bool` | | a closed set of string literals, `"horizontal" \| "vertical"` | a polymorphic variant, `` [ `horizontal \| `vertical ] `` | | `number` | `float`, since a slider's value is fractional | -| `T \| null` | `T Js.Nullable.t`: Kobalte reads `undefined` as uncontrolled and `null` as controlled and empty | +| `T \| null`, given | `T Js.Null.t`: Kobalte reads `undefined` as uncontrolled and `null` as controlled and empty, and `Js.Null.t` cannot hold `undefined` | +| `T \| null`, read | `T Js.Nullable.t`, which takes whatever JavaScript gives | | an optional prop, `T \| undefined` | an optional argument, `?name` | | a callback's optional argument | an `option` | | `A \| B` of different JavaScript kinds | `(a, b) Types.Either.t`, built by `Either.left` and `Either.right` | @@ -103,7 +104,7 @@ are Kobalte's building blocks -- menu, popper, dismissable layer, polymorphic -- which its documentation does not offer as components. `Root` and `Root_multiple` show why a type is split rather than widened. A -toggle group's value is one `string`, or nothing, `string Js.Nullable.t`; +toggle group's value is one `string`, or nothing, `string Js.Null.t`; with `multiple`, it is a `string array`. TypeScript says which by overloading on `multiple`, which OCaml cannot. Two modules give each its own exact type. [Using Kobalte](/solidtype/kobalte/usage/#toggle-group) shows both. diff --git a/docs/src/content/docs/kobalte/usage.mdx b/docs/src/content/docs/kobalte/usage.mdx index 94632eb..b0c177f 100644 --- a/docs/src/content/docs/kobalte/usage.mdx +++ b/docs/src/content/docs/kobalte/usage.mdx @@ -36,8 +36,9 @@ reaches, so the control's focus ring is styled from it, `peer-focus-visible`. `ToggleGroup.Root` holds one value, or none: its `value` is a -`string Js.Nullable.t`, since Kobalte reads `undefined` as uncontrolled and -`null` as a controlled group with nothing pressed. `ToggleGroup.Root_multiple` +`string Js.Null.t`, since Kobalte reads `undefined` as uncontrolled and +`null` as a controlled group with nothing pressed. `on_change` tells a +`string Js.Nullable.t`, as Kobalte gives it. `ToggleGroup.Root_multiple` holds several, a `string array`, and takes `multiple=true`. Each has the exact type of its value, where TypeScript has one component whose type turns on `multiple`. diff --git a/docs/src/content/docs/start/ocaml-over-typescript.mdx b/docs/src/content/docs/start/ocaml-over-typescript.mdx index 37d463b..bd3c12f 100644 --- a/docs/src/content/docs/start/ocaml-over-typescript.mdx +++ b/docs/src/content/docs/start/ocaml-over-typescript.mdx @@ -207,17 +207,19 @@ Kobalte reads a `value` of `undefined` as uncontrolled, and `null` as controlled and empty. In TSX, `value={selected()}` compiles where `selected()` may be `undefined`, and the moment it is, the select keeps a value of its own and stops following the page. In OCaml a value Kobalte -reads as `null` is a `Js.Nullable.t`, so an empty selection says so: +reads as `null` is a `Js.Null.t`, which cannot hold `undefined`, so an +empty selection says so: ```ocaml let picker () = + value=(Js.Null.fromOption (S.get selected)) /> ``` -`None` is `null`, still controlled. Handing the value to Kobalte is leaving -the prop out. +`None` is `null`, still controlled. `Js.Nullable.fromOption`, whose `None` +is `undefined`, does not compile there. Handing the value to Kobalte is +leaving the prop out. ### It is Solid underneath diff --git a/generate/binding.ts b/generate/binding.ts index 3117919..4ba8c08 100644 --- a/generate/binding.ts +++ b/generate/binding.ts @@ -196,6 +196,31 @@ function elementBound(t: ts.Type, vars: Vars): Type | null { return dom ? apply("Dom.element_like", typeVar(t.symbol?.name ?? "t", vars)) : null; } +// Which way a value being mapped crosses: given to JavaScript, as a prop is; +// read from it, as a function's result is; or both, as a field its object's +// constructor takes and its getter reads. A function's parameters cross the +// other way to the function itself. +type Polarity = "given" | "read" | "both"; +let polarity: Polarity = "both"; +const flip = (p: Polarity): Polarity => (p === "given" ? "read" : p === "read" ? "given" : "both"); +function crossing(p: Polarity, f: () => T): T { + const outer = polarity; + polarity = p; + try { + return f(); + } finally { + polarity = outer; + } +} + +// TypeScript's null, by AGENTS.md's "Kobalte's binding is generated from its +// declarations": given, `T | null` is Js.Null.t, which cannot hold the +// undefined a library reads as no value given, so an option is made one only +// by Js.Null.fromOption, whose None is null; read, or `T | null | undefined` +// given, it is Js.Nullable.t, which takes whatever JavaScript holds. +const nullOf = (undefinable: boolean) => + polarity === "given" && !undefinable ? "Js.Null.t" : "Js.Nullable.t"; + // [absent] drops undefined, which an optional prop's absence already says; // anywhere else -- a callback's argument, a result -- it is an option. // The OCaml type of a TypeScript one, by AGENTS.md's "Kobalte's binding is @@ -227,7 +252,7 @@ function map(t: ts.Type, vars: Vars, where: string, absent = false): Type | null if (!isJsx) members = members.filter((m) => !(m.flags & ts.TypeFlags.Null)); const inner = mapUnion(members, vars, where); if (!inner) return null; - const value = nullable ? apply("Js.Nullable.t", inner) : inner; + const value = nullable ? apply(nullOf(undefinable && !absent), inner) : inner; return undefinable && !absent && !isJsx && !isUnit(inner) && !nullable ? apply("option", value) : value; } return mapUnion([t], vars, where); @@ -495,7 +520,8 @@ function mapFunction(sig: ts.Signature, vars: Vars, where: string, labelled = fa const params: (Param | null)[] = ps.map((q) => { const optional = labelled && leftOut(q); const t = checker.getTypeOfSymbol(q); - const m = declaredPorted(q, vars, where, optional) ?? map(optional ? checker.getNonNullableType(t) : t, vars, where); + const m = crossing(flip(polarity), () => + declaredPorted(q, vars, where, optional) ?? map(optional ? checker.getNonNullableType(t) : t, vars, where)); if (m === null) return null; return optional ? { label: value(snake(q.name)), optional: true, type: m } : { type: m }; }); @@ -602,7 +628,8 @@ export function part(src: Source, jsModule: string, where: string, decl: ts.Sign const t: Type | null = p.name === "ref" ? refOf(pt, vars, `${where}.${p.name}`) - : (declaredPorted(p, vars, `${where}.${p.name}`, optional) ?? map(pt, vars, `${where}.${p.name}`, optional)); + : crossing("given", () => + declaredPorted(p, vars, `${where}.${p.name}`, optional) ?? map(pt, vars, `${where}.${p.name}`, optional)); if (!t) { unmapped.set(`${where}.${p.name}`, checker.typeToString(pt)); continue; @@ -700,7 +727,7 @@ export const real = (s: ts.Symbol) => (s.flags & ts.SymbolFlags.Alias ? checker. // one that may throw under a wrapper that raises, with an _opt beside it. export function functionBinding(src: Source, symbol: ts.Symbol, jsName: string, jsModule: string, where: string): void { const sig = checker.getSignaturesOfType(checker.getTypeOfSymbol(symbol), ts.SignatureKind.Call)[0]; - const t = sig ? mapFunction(sig, new Map(), where, true) : null; + const t = sig ? crossing("read", () => mapFunction(sig, new Map(), where, true)) : null; if (!t) { unmapped.set(where, sig ? checker.signatureToString(sig) : "?"); return; @@ -761,7 +788,7 @@ export function valueBinding(src: Source, symbol: ts.Symbol, jsName: string, jsM // A value: toaster, the storage managers, the default translations. const vt = checker.getTypeOfSymbol(symbol); if (vt.getCallSignatures().length > 0 && vt.getProperties().length > 0) return; - const t = map(vt, new Map(), where); + const t = crossing("read", () => map(vt, new Map(), where)); if (!t || typeVarsOf(t).length > 0) { unmapped.set(where, checker.typeToString(vt)); return; @@ -822,7 +849,7 @@ while (queue.length > 0) { // The standard library's methods are Melange's to bind. if (library) continue; const sig = pt.getCallSignatures()[0]; - const f = sig ? mapFunction(sig, vars, `${n.module}.${p.name}`, true) : null; + const f = sig ? crossing("read", () => mapFunction(sig, vars, `${n.module}.${p.name}`, true)) : null; if (!f || unbound(f, params)) { unmapped.set(`${n.module}.${p.name}()`, sig ? checker.signatureToString(sig) : "?"); continue; @@ -884,7 +911,7 @@ while (queue.length > 0) { // Called as a method of the object that holds it: an external is // called directly from another library, where a [let] in a nested // module goes through Curry, its arity lost. - src.both(` external ${value(snake(p.name))} : ${t} -> ${showIn(mapFunction(sig, vars, `${n.module}.${p.name}`, true) ?? fn)} = "${p.name}" [@@mel.send]`); + src.both(` external ${value(snake(p.name))} : ${t} -> ${showIn(crossing("read", () => mapFunction(sig, vars, `${n.module}.${p.name}`, true)) ?? fn)} = "${p.name}" [@@mel.send]`); } } else { src.both(` external ${value(snake(p.name))} : ${t} -> ${optional ? `${argIn(ft)} option` : argIn(ft)} = "${p.name}" [@@mel.get]`); @@ -934,7 +961,7 @@ while (queue.length > 0) { if (isClass && n.symbol?.valueDeclaration) { const mod = moduleOf(n.symbol); const ctor = checker.getTypeOfSymbolAtLocation(n.symbol, n.symbol.valueDeclaration).getConstructSignatures()[0]; - const f = ctor ? mapFunction(ctor, vars, `${n.module}.constructor`, true) : null; + const f = ctor ? crossing("read", () => mapFunction(ctor, vars, `${n.module}.constructor`, true)) : null; if (f && mod) { src.both(` external make : ${showIn({ ...f, result: self })} = "${n.module}" [@@mel.new] [@@mel.module "${mod}"]`); } diff --git a/headless/calendar.mli b/headless/calendar.mli index 8c40580..dc435e9 100644 --- a/headless/calendar.mli +++ b/headless/calendar.mli @@ -157,22 +157,21 @@ end (** A calendar of one date, a [
] named by [aria_label], else [aria_labelledby], and the dates it shows. Its value is [value] while - it is given one -- [Js.Nullable.null] keeps it empty -- else the one it - keeps, [default_value] at first; each date chosen calls [on_change]. Paging - moves by [visible_duration], or a single month, week or day with - [page_behavior] [`single]; [selection_alignment] places the focused date at - the start, the centre or the end of what is shown. Its weeks start on - [first_day_of_week], else the locale's, and a month's grid has - [weeks_in_month] rows, else as many as the month needs. While [is_disabled] - nothing is focused or chosen, and while [is_read_only] days are focused but - not chosen; it is invalid while [is_invalid] or while its value is out of - its range or unavailable. *) + it is given one -- [Js.Null.empty] keeps it empty -- else the one it keeps, + [default_value] at first; each date chosen calls [on_change]. Paging moves + by [visible_duration], or a single month, week or day with [page_behavior] + [`single]; [selection_alignment] places the focused date at the start, the + centre or the end of what is shown. Its weeks start on [first_day_of_week], + else the locale's, and a month's grid has [weeks_in_month] rows, else as + many as the month needs. While [is_disabled] nothing is focused or chosen, + and while [is_read_only] days are focused but not chosen; it is invalid + while [is_invalid] or while its value is out of its range or unavailable. *) module Root : sig module Props : sig type t val check : - ?value:D.Types.CalendarDate.t Js.Nullable.t Solidtype.Check.value -> + ?value:D.Types.CalendarDate.t Js.Null.t Solidtype.Check.value -> ?default_value:D.Types.CalendarDate.t Solidtype.Check.value -> ?on_change:(D.Types.CalendarDate.t -> unit) -> ?focused_value:D.Types.CalendarDate.t Solidtype.Check.value -> @@ -261,7 +260,7 @@ module Root_range : sig type t val check : - ?value:range Js.Nullable.t Solidtype.Check.value -> + ?value:range Js.Null.t Solidtype.Check.value -> ?default_value:range Solidtype.Check.value -> ?on_change:(range option -> unit) -> ?allows_non_contiguous_ranges:bool Solidtype.Check.value -> diff --git a/headless/calendar.mlx b/headless/calendar.mlx index 5005ffb..4d9721d 100644 --- a/headless/calendar.mlx +++ b/headless/calendar.mlx @@ -1906,7 +1906,7 @@ module Root = struct type t = Common.t external check : - ?value:C.t Js.Nullable.t Solidtype.Check.value -> + ?value:C.t Js.Null.t Solidtype.Check.value -> ?default_value:C.t Solidtype.Check.value -> ?on_change:(C.t -> unit) -> ?focused_value:C.t Solidtype.Check.value -> @@ -1934,7 +1934,7 @@ module Root = struct Solidtype.Html.Div.check = "check" [@@mel.module "vite-plugin-solidtype/markers"] - external value : t -> C.t Js.Nullable.t option = "value" [@@mel.get] + external value : t -> C.t Js.Null.t option = "value" [@@mel.get] external default_value : t -> C.t option = "defaultValue" [@@mel.get] external on_change : t -> ((C.t -> unit)[@u]) option = "onChange" @@ -1953,7 +1953,7 @@ module Root = struct in let current () = match (Props.value props, picker) with - | Some v, _ -> Js.Nullable.toOption v + | Some v, _ -> Js.Null.toOption v | None, Some (v, _) -> S.get v | None, None -> S.get kept in @@ -2062,7 +2062,7 @@ module Root_range = struct type t = Common.t external check : - ?value:range Js.Nullable.t Solidtype.Check.value -> + ?value:range Js.Null.t Solidtype.Check.value -> ?default_value:range Solidtype.Check.value -> ?on_change:(range option -> unit) -> ?allows_non_contiguous_ranges:bool Solidtype.Check.value -> @@ -2092,7 +2092,7 @@ module Root_range = struct Solidtype.Html.Div.check = "check" [@@mel.module "vite-plugin-solidtype/markers"] - external value : t -> range Js.Nullable.t option = "value" [@@mel.get] + external value : t -> range Js.Null.t option = "value" [@@mel.get] external default_value : t -> range option = "defaultValue" [@@mel.get] external on_change : t -> ((range option -> unit)[@u]) option = "onChange" @@ -2118,7 +2118,7 @@ module Root_range = struct in let current () = match (Props.value props, picker) with - | Some v, _ -> Js.Nullable.toOption v + | Some v, _ -> Js.Null.toOption v | None, Some (v, _) -> S.get v | None, None -> S.get kept in diff --git a/headless/color_channel_field.mli b/headless/color_channel_field.mli index 3d913af..3ead230 100644 --- a/headless/color_channel_field.mli +++ b/headless/color_channel_field.mli @@ -43,7 +43,7 @@ module Root : sig type t val check : - ?value:Solidtype_kobalte.Types.Color.t Js.Nullable.t Solidtype.Check.value -> + ?value:Solidtype_kobalte.Types.Color.t Js.Null.t Solidtype.Check.value -> ?default_value:Solidtype_kobalte.Types.Color.t Solidtype.Check.value -> ?on_change:(Solidtype_kobalte.Types.Color.t option -> unit) -> channel: diff --git a/headless/color_channel_field.mlx b/headless/color_channel_field.mlx index 2d2095a..66f7144 100644 --- a/headless/color_channel_field.mlx +++ b/headless/color_channel_field.mlx @@ -52,7 +52,7 @@ module Root = struct type t external check : - ?value:Color.t Js.Nullable.t Solidtype.Check.value -> + ?value:Color.t Js.Null.t Solidtype.Check.value -> ?default_value:Color.t Solidtype.Check.value -> ?on_change:(Color.t option -> unit) -> channel:channel Solidtype.Check.value -> @@ -79,7 +79,7 @@ module Root = struct [@@mel.module "vite-plugin-solidtype/markers"] external channel : t -> channel = "channel" [@@mel.get] - external value : t -> Color.t Js.Nullable.t option = "value" [@@mel.get] + external value : t -> Color.t Js.Null.t option = "value" [@@mel.get] external default_value : t -> Color.t option = "defaultValue" [@@mel.get] external color_space : t -> [ `rgb | `hsl | `hsb ] option = "colorSpace" @@ -102,7 +102,7 @@ module Root = struct let current () = match (Props.value props, picker) with | Some v, _ -> ( - match Js.Nullable.toOption v with + match Js.Null.toOption v with | Some _ as given -> given | None -> S.get kept) | None, Some p -> Some (S.get p.color) diff --git a/headless/color_field.mli b/headless/color_field.mli index da68e3b..b8b6a6e 100644 --- a/headless/color_field.mli +++ b/headless/color_field.mli @@ -40,7 +40,7 @@ module Root : sig type t val check : - ?value:Solidtype_kobalte.Types.Color.t Js.Nullable.t Solidtype.Check.value -> + ?value:Solidtype_kobalte.Types.Color.t Js.Null.t Solidtype.Check.value -> ?default_value:Solidtype_kobalte.Types.Color.t Solidtype.Check.value -> ?on_change:(Solidtype_kobalte.Types.Color.t option -> unit) -> ?change_on_wheel:bool Solidtype.Check.value -> diff --git a/headless/color_field.mlx b/headless/color_field.mlx index 9259091..e5651f6 100644 --- a/headless/color_field.mlx +++ b/headless/color_field.mlx @@ -60,7 +60,7 @@ module Root = struct type t external check : - ?value:Color.t Js.Nullable.t Solidtype.Check.value -> + ?value:Color.t Js.Null.t Solidtype.Check.value -> ?default_value:Color.t Solidtype.Check.value -> ?on_change:(Color.t option -> unit) -> ?change_on_wheel:bool Solidtype.Check.value -> @@ -75,7 +75,7 @@ module Root = struct Solidtype.Html.Div.check = "check" [@@mel.module "vite-plugin-solidtype/markers"] - external value : t -> Color.t Js.Nullable.t option = "value" [@@mel.get] + external value : t -> Color.t Js.Null.t option = "value" [@@mel.get] external default_value : t -> Color.t option = "defaultValue" [@@mel.get] external on_change : t -> ((Color.t option -> unit)[@u]) option = "onChange" @@ -94,7 +94,7 @@ module Root = struct let picker = Color_picker.Context.use () in let colour () = match (Props.value props, picker) with - | Some v, _ -> Js.Nullable.toOption v + | Some v, _ -> Js.Null.toOption v | None, Some p -> Some (S.get p.color) | None, None -> S.get kept in diff --git a/headless/command.mli b/headless/command.mli new file mode 100644 index 0000000..2ba9a52 --- /dev/null +++ b/headless/command.mli @@ -0,0 +1,221 @@ +(** A command palette, as Kobalte would make one: a search whose text filters a + menu of commands, made of {!Solidtype_kobalte.Selection}'s hooks, as React + Aria's autocomplete filters its menu. Its {!Input} is an + [], its {!List} a [
] of {!Item}s, each + a [
], gathered in {!Group}s named by their {!Heading} + and parted by {!Separator}s. It draws no style: each part is the element it + names, its roles and state as attributes, and takes every attribute its + element does -- a class, a style, a [ref], a handler -- which it passes on, + a [ref] and a handler called beside its own. + + {[ + + + run key)> + + "Files" + "Create new file..." + + + "Change theme" + + + ]} + + The focus stays in the input. Down and Up move through the items shown, a + disabled one passed over, wrapping at either end; Home and End reach the + first and the last, Page Down and Page Up a list's height on. Typing shows + the items whose text holds the search's and gives the first of them the + focus; deleting leaves none focused. Enter or a press runs the item focused, + and the pointer focuses the item it is over. Escape empties the search, and + once it is empty is left to what is around the palette, a dialog that + closes. An item focused is [data-highlighted], and a disabled one + [aria-disabled] and [data-disabled], present and empty as Kobalte's state + is; a group whose items are all hidden is hidden with them, and a separator + that parts nothing is hidden. A list showing no item is [data-empty]. + + A part is used inside a {!Root}, a {!Heading} inside a {!Group}: elsewhere + it raises [Invalid_argument] as it is made. *) + +module S := Solidtype.Solid +module Jsx := Solidtype.Jsx + +type translations = { suggestions : string; clear_search : string } +(** What the palette tells a reader, in its locale's words, React Aria's, else + English: its list's name, and a clear button's. *) + +val english : translations +(** React Aria's English: [Suggestions], [Clear search]. *) + +val translations_for : string -> translations +(** React Aria's words for a locale, ["fr-FR"] or ["fr"], else English. *) + +type state = { + input_value : string S.accessor; (** The search's text. *) + set_input_value : string -> unit; + (** Sets the search's text, as typing does. *) + is_empty : bool S.accessor; (** Whether the list shows no item. *) + focused_key : string option S.accessor; + (** The item focused, by its key, as the keys and the pointer move it. *) + is_disabled : string -> bool; + (** Whether the list disables an item, by its key. *) + focus_input : unit -> unit; (** Gives the search the focus. *) + translations : translations S.accessor; +} +(** What a part inside a {!Root} reads of the palette, and a style's attribute + on it. *) + +module Context : sig + val use : unit -> state + (** The palette above; outside a {!Root} it raises [Invalid_argument]. *) +end + +(** The palette, which draws no element. The search's text is [input_value] + while it is given, else the text it keeps, [default_input_value] at first; a + change calls [on_input_change]. An item is shown while [filter] keeps it, + given its text and the search's, else while its text holds the search's, + whatever their case or accents. Its words are [translations] where given, + else its locale's. *) +module Root : sig + module Props : sig + type t + + val check : + ?input_value:string Solidtype.Check.value -> + ?default_input_value:string Solidtype.Check.value -> + ?on_input_change:(string -> unit) -> + ?filter:(string -> string -> bool) -> + ?translations:translations Solidtype.Check.value -> + children:(_, [ `elements ]) Solidtype.Check.children -> + ?props:t S.rest -> + unit -> + unit + end + + val make : Props.t -> Jsx.element + val component : Props.t Jsx.component +end + +(** The search: an [] whose text is the palette's, which + controls its list and names the item focused as its active descendant. *) +module Input : sig + module Props : sig + type t + + val check : + ?as_:(Solidtype.Html.Input.props -> Jsx.element) -> + ?props:'spread S.rest -> + (unit -> unit) Solidtype.Html.Input.check + end + + val make : Props.t -> Jsx.element + val component : Props.t Jsx.component +end + +(** The items: a [
] named by the palette's words, + [Suggestions], unless it is given a name. A press on an item, or Enter while + it is focused, calls the item's [on_action] and then this [on_action] with + its key. An item whose key [disabled_keys] holds is disabled. While it shows + no item it draws [render_empty_state]. *) +module List : sig + module Props : sig + type t + + val check : + ?on_action:(string -> unit) -> + ?disabled_keys:string array Solidtype.Check.value -> + ?render_empty_state:(unit -> Jsx.element) -> + ?as_:(Solidtype.Html.Div.props -> Jsx.element) -> + ?props:'spread S.rest -> + (?children:('f, [ `elements ]) Solidtype.Check.children -> unit -> unit) + Solidtype.Html.Div.check + end + + val make : Props.t -> Jsx.element + val component : Props.t Jsx.component +end + +type item = { + key : string S.accessor; + is_focused : bool S.accessor; + is_disabled : bool S.accessor; +} +(** What a part inside an {!Item} reads of it, and a style's attribute on it. *) + +(** A command: a [
], its key [id], else one it makes. It is + shown while the palette's filter keeps its [text_value], else the text it + draws; a press, or Enter while it is focused, calls [on_action]. It is + disabled while [is_disabled]. *) +module Item : sig + module Context : sig + val use : unit -> item + (** The item above; outside an {!Item} it raises [Invalid_argument]. *) + end + + module Props : sig + type t + + val check : + ?id:string Solidtype.Check.value -> + ?text_value:string Solidtype.Check.value -> + ?is_disabled:bool Solidtype.Check.value -> + ?on_action:(unit -> unit) -> + ?as_:(Solidtype.Html.Div.props -> Jsx.element) -> + ?props:'spread S.rest -> + (?children:('f, [ `elements ]) Solidtype.Check.children -> unit -> unit) + Solidtype.Html.Div.check + end + + val make : Props.t -> Jsx.element + val component : Props.t Jsx.component +end + +(** Items that belong together: a [
] named by its + {!Heading}, shown while one of its items is. *) +module Group : sig + module Props : sig + type t + + val check : + ?as_:(Solidtype.Html.Section.props -> Jsx.element) -> + ?props:'spread S.rest -> + (?children:('f, [ `elements ]) Solidtype.Check.children -> unit -> unit) + Solidtype.Html.Section.check + end + + val make : Props.t -> Jsx.element + val component : Props.t Jsx.component +end + +(** A group's name: a [
] the group is labelled by. +*) +module Heading : sig + module Props : sig + type t + + val check : + ?as_:(Solidtype.Html.Header.props -> Jsx.element) -> + ?props:'spread S.rest -> + (?children:('f, [ `elements ]) Solidtype.Check.children -> unit -> unit) + Solidtype.Html.Header.check + end + + val make : Props.t -> Jsx.element + val component : Props.t Jsx.component +end + +(** A line between items or groups: a [
], hidden while no + item is shown before it, or none after it, or it follows another. *) +module Separator : sig + module Props : sig + type t + + val check : + ?as_:(Solidtype.Html.Div.props -> Jsx.element) -> + ?props:'spread S.rest -> + (unit -> unit) Solidtype.Html.Div.check + end + + val make : Props.t -> Jsx.element + val component : Props.t Jsx.component +end diff --git a/headless/command.mlx b/headless/command.mlx new file mode 100644 index 0000000..7fe9bfb --- /dev/null +++ b/headless/command.mlx @@ -0,0 +1,974 @@ +module S = Solidtype.Solid +module Jsx = Solidtype.Jsx +module Show = Solidtype.Show +module Selection = Solidtype_kobalte.Selection +module I18n = Solidtype_kobalte.I18n +module Types = Solidtype_kobalte.Types +module Either = Types.Either +module Delegate = Types.KeyboardDelegate +module List_state = Types.ListState +module Manager = Types.MultipleSelectionManager +module Selectable_list = Types.Selection_CreateSelectableList + +(* Whether [other] follows [e] on the page, by DOM's bit for it. *) +external position : _ Dom.element_like -> _ Dom.element_like -> int + = "compareDocumentPosition" +[@@mel.send] + +type rect + +external bounds : _ Dom.element_like -> rect = "getBoundingClientRect" +[@@mel.send] + +external top : rect -> float = "top" [@@mel.get] + +external client_height : _ Dom.element_like -> float = "clientHeight" +[@@mel.get] + +external pressed : Dom.keyboardEvent -> string = "key" [@@mel.get] +external shift : Dom.keyboardEvent -> bool = "shiftKey" [@@mel.get] +external composing : Dom.keyboardEvent -> bool = "isComposing" [@@mel.get] +external prevent : Dom.keyboardEvent -> unit = "preventDefault" [@@mel.send] +external prevent_mouse : Dom.mouseEvent -> unit = "preventDefault" [@@mel.send] + +external input_target : Dom.inputEvent -> Dom.htmlInputElement = "target" +[@@mel.get] + +external input_value : Dom.htmlInputElement -> string = "value" [@@mel.get] + +(* How the text changed, an InputEvent's; [undefined] where the event is not + one, which is no typing forward. *) +external input_type : Dom.inputEvent -> string Js.undefined = "inputType" +[@@mel.get] + +external focus : Dom.htmlInputElement -> unit = "focus" [@@mel.send] +external pointer_type : Dom.pointerEvent -> string = "pointerType" [@@mel.get] + +(* Kobalte's state, an attribute present and empty, or none. *) +let state on = if on then Jsx.data "" else Jsx.flag false + +type translations = { suggestions : string; clear_search : string } + +let english = { suggestions = "Suggestions"; clear_search = "Clear search" } + +let translated = + [ + ( "ar-AE", + { suggestions = {js|مقترحات|js}; clear_search = {js|مسح البحث|js} } ); + ( "bg-BG", + { + suggestions = {js|Предложения|js}; + clear_search = {js|Изчистване на търсене|js}; + } ); + ( "cs-CZ", + { suggestions = {js|Návrhy|js}; clear_search = {js|Vymazat hledání|js} } + ); + ("da-DK", { suggestions = "Forslag"; clear_search = {js|Ryd søgning|js} }); + ( "de-DE", + { + suggestions = "Empfehlungen"; + clear_search = {js|Suche zurücksetzen|js}; + } ); + ( "el-GR", + { + suggestions = {js|Προτάσεις|js}; + clear_search = {js|Απαλοιφή αναζήτησης|js}; + } ); + ("en-US", english); + ( "es-ES", + { suggestions = "Sugerencias"; clear_search = {js|Borrar búsqueda|js} } ); + ( "et-EE", + { suggestions = "Soovitused"; clear_search = {js|Tühjenda otsing|js} } ); + ( "fi-FI", + { suggestions = "Ehdotukset"; clear_search = {js|Tyhjennä haku|js} } ); + ( "fr-FR", + { suggestions = "Suggestions"; clear_search = "Effacer la recherche" } ); + ("he-IL", { suggestions = {js|הצעות|js}; clear_search = {js|נקה חיפוש|js} }); + ( "hr-HR", + { suggestions = "Prijedlozi"; clear_search = {js|Obriši pretragu|js} } ); + ( "hu-HU", + { suggestions = "Javaslatok"; clear_search = {js|Keresés törlése|js} } ); + ( "it-IT", + { suggestions = "Suggerimenti"; clear_search = "Cancella ricerca" } ); + ("ja-JP", { suggestions = {js|候補|js}; clear_search = {js|検索をクリア|js} }); + ("ko-KR", { suggestions = {js|제안|js}; clear_search = {js|검색 지우기|js} }); + ( "lt-LT", + { + suggestions = {js|Pasiūlymai|js}; + clear_search = {js|Išvalyti iešką|js}; + } ); + ( "lv-LV", + { suggestions = "Ieteikumi"; clear_search = {js|Notīrīt meklēšanu|js} } ); + ("nb-NO", { suggestions = "Forslag"; clear_search = {js|Tøm søk|js} }); + ("nl-NL", { suggestions = "Suggesties"; clear_search = "Zoekactie wissen" }); + ( "pl-PL", + { + suggestions = "Sugestie"; + clear_search = {js|Wyczyść zawartość wyszukiwania|js}; + } ); + ( "pt-BR", + { suggestions = {js|Sugestões|js}; clear_search = "Limpar pesquisa" } ); + ( "pt-PT", + { suggestions = {js|Sugestões|js}; clear_search = "Limpar pesquisa" } ); + ( "ro-RO", + { suggestions = "Sugestii"; clear_search = {js|Ştergeţi căutarea|js} } ); + ( "ru-RU", + { + suggestions = {js|Предложения|js}; + clear_search = {js|Очистить поиск|js}; + } ); + ( "sk-SK", + { + suggestions = {js|Návrhy|js}; + clear_search = {js|Vymazať vyhľadávanie|js}; + } ); + ( "sl-SI", + { suggestions = "Predlogi"; clear_search = {js|Počisti iskanje|js} } ); + ( "sr-SP", + { suggestions = "Predlozi"; clear_search = {js|Obriši pretragu|js} } ); + ( "sv-SE", + { suggestions = {js|Förslag|js}; clear_search = {js|Rensa sökning|js} } ); + ( "tr-TR", + { suggestions = {js|Öneriler|js}; clear_search = {js|Aramayı temizle|js} } + ); + ( "uk-UA", + { + suggestions = {js|Пропозиції|js}; + clear_search = {js|Очистити пошук|js}; + } ); + ("zh-CN", { suggestions = {js|建议|js}; clear_search = {js|清除搜索|js} }); + ("zh-TW", { suggestions = {js|建議|js}; clear_search = {js|清除搜尋條件|js} }); + ] + +let locale_language locale = + match String.index_opt locale '-' with + | Some i -> String.sub locale 0 i + | None -> locale + +(* A locale's entry of [table]: its own, else its language's, else the first + of its language, else [default], as \@internationalized/string finds a + locale's strings. *) +let localized table locale ~default = + match List.assoc_opt locale table with + | Some p -> p + | None -> ( + let language = locale_language locale in + match List.assoc_opt language table with + | Some p -> p + | None -> ( + let prefix = language ^ "-" in + let n = String.length prefix in + match + List.find_opt + (fun (k, _) -> + String.length k > n && String.equal (String.sub k 0 n) prefix) + table + with + | Some (_, p) -> p + | None -> default)) + +let translations_for locale = localized translated locale ~default:english + +(* A provider's locale, or en-GB, so a page reads the same on the server and + in every browser. *) +let use_locale () = + let i18n = I18n.use_locale () in + let provided = I18n.use_locale () == i18n in + S.accessor (fun () -> + if provided then Types.I18n_UseLocale.locale i18n () else "en-GB") + +type state = { + input_value : string S.accessor; + set_input_value : string -> unit; + is_empty : bool S.accessor; + focused_key : string option S.accessor; + is_disabled : string -> bool; + focus_input : unit -> unit; + translations : translations S.accessor; +} + +type item = { + key : string S.accessor; + is_focused : bool S.accessor; + is_disabled : bool S.accessor; +} + +(* An item as the palette keeps it while it is shown: its key, its element, + which says where it is on the page, and what runs it. *) +type entry = { + entry_key : string S.accessor; + id : string; + element : Dom.htmlDivElement; + disabled : bool S.accessor; + run : unit -> unit; +} + +(* What its list gives the palette: what runs as an item does, and which keys + it disables. *) +type list_given = { + act : string -> unit; + disabled_keys : unit -> string array option; +} + +type palette = { + text : string S.accessor; + set_text : string -> unit; + keeps : string -> bool; + translations_ : translations S.accessor; + list : Selectable_list.t; + input : Dom.htmlInputElement option S.accessor; + set_input : Dom.htmlInputElement option S.setter; + list_element : Dom.htmlDivElement option S.accessor; + set_list_element : Dom.htmlDivElement option S.setter; + list_id : string; + focused : string option S.accessor; + set_focused : string option -> unit; + shown : unit -> entry array; + is_empty_ : bool S.accessor; + add : entry -> unit; + drop : Dom.htmlDivElement -> unit; + add_separator : Dom.htmlDivElement -> unit; + drop_separator : Dom.htmlDivElement -> unit; + parting : Dom.htmlDivElement array S.accessor; + set_given : list_given -> unit; + act : string -> unit; + disabled_key : string -> bool; + item_id : string -> string option; + run_focused : unit -> bool; +} + +module Palette_context = Solidtype.Context.Make_optional (struct + type t = palette +end) + +(* A group as its items and heading read it: each item counts itself shown, + and its heading names it. *) +type group = { + count : bool S.accessor -> unit; + heading_id : string; + heading_here : unit -> unit; +} + +module Group_context = Solidtype.Context.Make_optional (struct + type t = group +end) + +module Item_context = Solidtype.Context.Make_optional (struct + type t = item +end) + +let in_palette part = + match Palette_context.use () with + | Some p -> p + | None -> invalid_arg (part ^ " is used inside a Command.Root") + +module Context = struct + let use () = + let p = in_palette "Command.Context.use" in + { + input_value = p.text; + set_input_value = p.set_text; + is_empty = p.is_empty_; + focused_key = p.focused; + is_disabled = p.disabled_key; + focus_input = (fun () -> Option.iter focus (S.get p.input)); + translations = p.translations_; + } +end + +(* An id on the page while the part that carries it is: a part counts itself + in as it is made and out as it goes. *) +let named id = + let count, set_count = S.create_signal 0 in + let here () = + S.set set_count (S.get count + 1); + S.on_cleanup (fun () -> S.set set_count (S.get count - 1)) + in + (here, fun () -> if S.get count > 0 then Some id else None) + +let in_order elements = + List.sort + (fun a b -> + if a == b then 0 else if position a b land 4 <> 0 then -1 else 1) + elements + +(* Where the keys move among the items shown: in their order on the page, a + disabled one passed over, a page the list's height on, as React Aria's menu + moves. Kobalte's list wraps at either end. *) +let delegate ~(shown : unit -> entry array) ~list_element = + let usable () = + Js.Array.filter ~f:(fun e -> not (S.get e.disabled)) (shown ()) + in + let index all k = + Js.Array.findIndex ~f:(fun e -> String.equal (S.get e.entry_key) k) all + in + let key e = Some (S.get e.entry_key) in + let first () = Option.bind (Array.find_opt (fun _ -> true) (usable ())) key in + let last () = + let all = usable () in + match Array.length all with 0 -> None | n -> key all.(n - 1) + in + let step k by = + let all = usable () in + match index all k with + | -1 -> None + | i when i + by >= 0 && i + by < Array.length all -> key all.(i + by) + | _ -> None + in + let page ~down k = + let all = usable () in + match (index all k, S.get list_element) with + | -1, _ | _, None -> None + | i, Some l -> + let height = client_height l in + let from = top (bounds all.(i).element) in + let within e = + let d = top (bounds e.element) -. from in + if down then d <= height else -.d <= height + in + let rec walk j best = + if j < 0 || j >= Array.length all then best + else if within all.(j) then walk (if down then j + 1 else j - 1) j + else best + in + let reached = walk i i in + if reached <> i then key all.(reached) + else if down then last () + else first () + in + Delegate.make + ~get_key_below:(fun k -> step k 1) + ~get_key_above:(fun k -> step k (-1)) + ~get_key_page_below:(page ~down:true) ~get_key_page_above:(page ~down:false) + ~get_first_key:(fun _ _ -> first ()) + ~get_last_key:(fun _ _ -> last ()) + () + +module Root = struct + let%component make ?(input_value : string option) + ?(default_input_value : string option) + ?(on_input_change : (string -> unit) option) + ?(filter : (string -> string -> bool) option) + ?(translations : translations option) ~(children : Jsx.element) () = + let kept, set_kept = + S.create_signal + (Option.value + (S.untrack (fun () -> S.get default_input_value)) + ~default:"") + in + let text = + S.accessor (fun () -> + match S.get input_value with Some v -> v | None -> S.get kept) + in + let set_text v = + S.set set_kept v; + ignore (on_input_change v : unit option) + in + (* An item's text holding the search's, whatever its case or accents, as + React Aria's filter judges it, unless a filter is given. *) + let contains = + I18n.create_filter + ~options:(Types.CollatorOptions.make ~sensitivity:`base ()) + () + in + let keeps item_text = + let typed = S.get text in + String.equal typed "" + || + match filter item_text typed with + | Some kept -> kept + | None -> Types.Filter.contains contains item_text typed + in + let locale = use_locale () in + let translations_ = + S.accessor (fun () -> + match S.get translations with + | Some t -> t + | None -> translations_for (S.get locale)) + in + let listed, set_listed = S.create_signal [] in + let separators, set_separators = S.create_signal [] in + let given, set_given = S.create_signal None in + let disabled_key k = + match S.get given with + | Some g -> ( + match g.disabled_keys () with + | Some keys -> Array.exists (String.equal k) keys + | None -> false) + | None -> false + in + let find k = + List.find_opt (fun e -> String.equal (S.get e.entry_key) k) (S.get listed) + in + (* The items shown in their order on the page, read as the keys move, + once each is in it. *) + let shown () = + let by_element = S.get listed in + in_order (List.map (fun e -> e.element) by_element) + |> List.filter_map (fun el -> + List.find_opt (fun e -> e.element == el) by_element) + |> Array.of_list + in + let keys = + S.create_memo (fun () -> + Array.of_list (List.map (fun e -> S.get e.entry_key) (S.get listed))) + in + let list_state = + Selection.create_list_state + (Types.CreateListStateProps.make ~data_source:(Either.right keys) + ~get_key:(Either.left Fun.id) + ~get_text_value:(Either.left (fun _ -> "")) + ~get_disabled: + (Either.left (fun k -> + match find k with Some e -> S.get e.disabled | None -> true)) + ~selection_mode:(Either.left `none) ()) + in + let manager = + S.create_memo (fun () -> + Types.SelectionManager.to_multiple_selection_manager + (S.get (List_state.selection_manager list_state))) + in + let input, set_input = S.create_signal None in + let list_element, set_list_element = S.create_signal None in + let list = + Selection.create_selectable_list + (Types.CreateSelectableListProps.make + ~collection:(List_state.collection list_state) + ~selection_manager:(Either.right manager) + ~keyboard_delegate:(Either.left (delegate ~shown ~list_element)) + ~should_focus_wrap:(Either.left true) + ~disallow_type_ahead:(Either.left true) + ~should_use_virtual_focus:(Either.left true) ()) + input ~scroll_ref:list_element () + in + let focused = + S.accessor (fun () -> S.get (Manager.focused_key (S.get manager))) + in + let set_focused key = Manager.set_focused_key (S.get manager) ?key () in + (* An item focused that is no longer shown leaves the focus with none, as + React Aria's autocomplete clears it. *) + S.create_effect (fun () -> + match S.get focused with + | Some k when not (Array.exists (String.equal k) (S.get keys)) -> + S.untrack (fun () -> set_focused None) + | Some _ | None -> ()); + (* A separator parts what is shown: it follows an item shown with no + other separator since, and an item shown follows it. *) + (* Where a part is on the page is read once the palette is on it: it is + made whole before it is put there. *) + let settled, set_settled = S.create_signal false in + S.on_mount (fun () -> S.set set_settled true); + let parting = + S.create_memo (fun () -> + if not (S.get settled) then [||] + else + let items = List.map (fun e -> e.element) (S.get listed) in + let seps = S.get separators in + let _, _, parting = + List.fold_left + (fun (seen, pending, parting) el -> + if List.exists (fun s -> s == el) seps then + if seen then (false, Some el, parting) + else (seen, pending, parting) + else + match pending with + | Some s -> (true, None, s :: parting) + | None -> (true, None, parting)) + (false, None, []) + (in_order (items @ seps)) + in + Array.of_list parting) + in + let p = + { + text; + set_text; + keeps; + translations_; + list; + input; + set_input; + list_element; + set_list_element; + list_id = S.create_unique_id (); + focused; + set_focused; + shown; + is_empty_ = S.accessor (fun () -> Array.length (S.get keys) = 0); + add = (fun e -> S.update set_listed (fun l -> e :: l)); + drop = + (fun el -> + S.update set_listed (List.filter (fun e -> e.element != el))); + add_separator = (fun el -> S.update set_separators (fun l -> el :: l)); + drop_separator = + (fun el -> S.update set_separators (List.filter (fun s -> s != el))); + parting; + set_given = (fun g -> S.set set_given (Some g)); + act = + (fun k -> + match S.untrack (fun () -> S.get given) with + | Some g -> g.act k + | None -> ()); + disabled_key; + item_id = (fun k -> Option.map (fun e -> e.id) (find k)); + run_focused = + (fun () -> + match Option.bind (S.get focused) find with + | Some e when not (S.get e.disabled) -> + e.run (); + true + | Some _ | None -> false); + } + in + + (S.get children) + +end + +(* A child's text: a string's own, an element's content, as a reader hears + the item. *) +external element_text : Jsx.element -> string Js.undefined = "textContent" +[@@mel.get] + +let text_of child = + match Js.Types.classify child with + | JSString s -> s + | JSObject _ -> + Option.value (Js.Undefined.toOption (element_text child)) ~default:"" + | JSFalse | JSTrue | JSNull | JSUndefined | JSNumber _ | JSFunction _ + | JSSymbol _ | JSBigInt _ -> + "" + +module Input = struct + module Props = struct + type t + + external check : + ?as_:(Solidtype.Html.Input.props -> Solidtype.Jsx.element) -> + ?props:'spread Solidtype.Solid.rest -> + (unit -> unit) Solidtype.Html.Input.check = "check" + [@@mel.module "vite-plugin-solidtype/markers"] + + external ref : t -> ((Dom.htmlInputElement -> unit)[@u]) option = "ref" + [@@mel.get] + + external on_input : t -> ((Dom.inputEvent -> unit)[@u]) option = "onInput" + [@@mel.get] + + external on_key_down : t -> ((Dom.keyboardEvent -> unit)[@u]) option + = "onKeyDown" + [@@mel.get] + end + + (* The props the input takes for itself; the rest it passes on. *) + let own = [| "ref"; "onInput"; "onKeyDown"; "value"; "type" |] + + let make (props : Props.t) = + let p = in_palette "Command.Input" in + let first () = + Array.find_opt (fun e -> not (S.get e.disabled)) (p.shown ()) + |> Option.map (fun e -> S.get e.entry_key) + in + let on_key_down e = + Jsx.call (Props.on_key_down props) e; + if not (composing e) then + match pressed e with + | "Escape" -> + (* A search with text is emptied, the key marked handled, so a + dialog around the palette stays open; an empty one leaves the + key to it. *) + if not (String.equal (S.get p.text) "") then ( + prevent e; + p.set_text ""; + p.set_focused None) + | "Enter" -> if p.run_focused () then prevent e + | ("Home" | "End") when shift e && Option.is_none (S.get p.focused) -> + () + | "ArrowDown" | "ArrowUp" | "Home" | "End" | "PageDown" | "PageUp" -> + Selectable_list.on_key_down p.list e + | "ArrowLeft" | "ArrowRight" -> + (* The caret moves, and no item keeps the focus, as React Aria's + autocomplete lets a reader hear the text. *) + p.set_focused None + | _ -> () + in + let on_input e = + Jsx.call (Props.on_input props) e; + p.set_text (input_value (input_target e)); + (* Typing forward gives the first item shown the focus; deleting, + pasting or undoing leaves none focused, as React Aria's autocomplete + does. *) + match Js.Undefined.toOption (input_type e) with + | Some ("insertText" | "insertCompositionText" | "insertFromComposition") + -> + p.set_focused (first ()) + | Some _ | None -> p.set_focused None + in + + S.set p.set_input (Some e); + S.on_cleanup (fun () -> S.set p.set_input None); + Jsx.call (Props.ref props) e) + value=(S.get p.text) + aria_autocomplete=`list + ?aria_controls=(if Option.is_some (S.get p.list_element) then + Some p.list_id + else None) + ?aria_activedescendant=(Option.bind (S.get p.focused) p.item_id) + autocomplete="off" + autocorrect=`off + spellcheck=false + enterkeyhint="go" + on_input + on_key_down + ?as_=(S.drawn_as props) + props=(S.undrawn (S.omit props own)) + /> + + let component = Jsx.component make +end + +module List = struct + module Props = struct + type t + + external check : + ?on_action:(string -> unit) -> + ?disabled_keys:string array Solidtype.Check.value -> + ?render_empty_state:(unit -> Solidtype.Jsx.element) -> + ?as_:(Solidtype.Html.Div.props -> Solidtype.Jsx.element) -> + ?props:'spread Solidtype.Solid.rest -> + (?children:('f, [ `elements ]) Solidtype.Check.children -> unit -> unit) + Solidtype.Html.Div.check = "check" + [@@mel.module "vite-plugin-solidtype/markers"] + + external on_action : t -> ((string -> unit)[@u]) option = "onAction" + [@@mel.get] + + external disabled_keys : t -> string array option = "disabledKeys" + [@@mel.get] + + external render_empty_state : t -> Jsx.element S.accessor option + = "renderEmptyState" + [@@mel.get] + + external children : t -> Jsx.element = "children" [@@mel.get] + + external ref : t -> ((Dom.htmlDivElement -> unit)[@u]) option = "ref" + [@@mel.get] + + external on_mouse_down : t -> ((Dom.mouseEvent -> unit)[@u]) option + = "onMouseDown" + [@@mel.get] + end + + (* The props the list takes for itself; the rest it passes on. *) + let own = + [| + "onAction"; + "disabledKeys"; + "renderEmptyState"; + "children"; + "ref"; + "onMouseDown"; + |] + + let make (props : Props.t) = + let p = in_palette "Command.List" in + p.set_given + { + act = (fun k -> Jsx.call (Props.on_action props) k); + disabled_keys = (fun () -> Props.disabled_keys props); + }; + let resolved = S.children (fun () -> Props.children props) in +
+ S.set p.set_list_element (Some e); + S.on_cleanup (fun () -> S.set p.set_list_element None); + Jsx.call (Props.ref props) e) + aria_label=(S.get p.translations_).suggestions + data_empty=(state (S.get p.is_empty_)) + on_mouse_down=(fun e -> + Jsx.call (Props.on_mouse_down props) e; + (* The focus stays in the search. *) + prevent_mouse e) + ?as_=(S.drawn_as props) + props=(S.undrawn (S.omit props own))> + (Jsx.fragment (S.to_array (S.get resolved))) + (match Props.render_empty_state props with + | Some draw when S.get p.is_empty_ -> + (* A menu holds items, so its empty state is one, as React Aria's + is. *) +
(S.get draw)
+ | Some _ | None -> Jsx.nothing) +
+ + let component = Jsx.component make +end + +module Item = struct + module Context = struct + let use () = + match Item_context.use () with + | Some t -> t + | None -> + invalid_arg "Command.Item.Context.use is used inside a Command.Item" + end + + module Props = struct + type t + + external check : + ?id:string Solidtype.Check.value -> + ?text_value:string Solidtype.Check.value -> + ?is_disabled:bool Solidtype.Check.value -> + ?on_action:(unit -> unit) -> + ?as_:(Solidtype.Html.Div.props -> Solidtype.Jsx.element) -> + ?props:'spread Solidtype.Solid.rest -> + (?children:('f, [ `elements ]) Solidtype.Check.children -> unit -> unit) + Solidtype.Html.Div.check = "check" + [@@mel.module "vite-plugin-solidtype/markers"] + + external id : t -> string option = "id" [@@mel.get] + external text_value : t -> string option = "textValue" [@@mel.get] + external is_disabled : t -> bool option = "isDisabled" [@@mel.get] + + external on_action : t -> ((unit -> unit)[@u]) option = "onAction" + [@@mel.get] + + external children : t -> Jsx.element = "children" [@@mel.get] + + external ref : t -> ((Dom.htmlDivElement -> unit)[@u]) option = "ref" + [@@mel.get] + + external on_click : t -> ((Dom.mouseEvent -> unit)[@u]) option = "onClick" + [@@mel.get] + + external on_mouse_down : t -> ((Dom.mouseEvent -> unit)[@u]) option + = "onMouseDown" + [@@mel.get] + + external on_pointer_enter : t -> ((Dom.pointerEvent -> unit)[@u]) option + = "onPointerEnter" + [@@mel.get] + end + + (* The props the item takes for itself; the rest it passes on. *) + let own = + [| + "id"; + "textValue"; + "isDisabled"; + "onAction"; + "children"; + "ref"; + "onClick"; + "onMouseDown"; + "onPointerEnter"; + |] + + let make (props : Props.t) = + let p = in_palette "Command.Item" in + let group = Group_context.use () in + let given_key = S.create_unique_id () in + let key = + S.create_memo (fun () -> Option.value (Props.id props) ~default:given_key) + in + let disabled = + S.accessor (fun () -> + Option.value (Props.is_disabled props) ~default:false + || p.disabled_key (S.get key)) + in + let is_focused = + S.accessor (fun () -> + match S.get p.focused with + | Some k -> String.equal k (S.get key) + | None -> false) + in + let t = { key; is_focused; is_disabled = disabled } in + (* Its children are made inside it, so a part among them reads the item + it is in, and kept while it is hidden, so it is shown again as it + was. *) + let resolved = + S.children (fun () -> + + (Props.children props) + ) + in + let text = + S.accessor (fun () -> + match Props.text_value props with + | Some t -> t + | None -> + String.trim + (String.concat "" + (Array.to_list + (Array.map text_of (S.to_array (S.get resolved)))))) + in + let shown = S.create_memo (fun () -> p.keeps (S.get text)) in + Option.iter (fun g -> g.count shown) group; + let id = S.create_unique_id () in + let run () = + if not (S.get disabled) then ( + Jsx.act (Props.on_action props); + p.act (S.get key)) + in + +
+ p.add { entry_key = key; id; element = e; disabled; run }; + S.on_cleanup (fun () -> p.drop e); + Jsx.call (Props.ref props) e) + ?aria_disabled=(if S.get disabled then Some true else None) + data_key=(Jsx.data (S.get key)) + data_highlighted=(state (S.get is_focused)) + data_disabled=(state (S.get disabled)) + on_pointer_enter=(fun e -> + Jsx.call (Props.on_pointer_enter props) e; + (* The pointer focuses the item it is over, as + React Aria's menu does, though not a touch. *) + if + (not (String.equal (pointer_type e) "touch")) + && not (S.get disabled) + then p.set_focused (Some (S.get key))) + on_mouse_down=(fun e -> + Jsx.call (Props.on_mouse_down props) e; + prevent_mouse e) + on_click=(fun e -> + Jsx.call (Props.on_click props) e; + run ()) + ?as_=(S.drawn_as props) + props=(S.undrawn (S.omit props own))> + (Jsx.fragment (S.to_array (S.get resolved))) +
+
+ + let component = Jsx.component make +end + +module Group = struct + module Props = struct + type t + + external check : + ?as_:(Solidtype.Html.Section.props -> Solidtype.Jsx.element) -> + ?props:'spread Solidtype.Solid.rest -> + (?children:('f, [ `elements ]) Solidtype.Check.children -> unit -> unit) + Solidtype.Html.Section.check = "check" + [@@mel.module "vite-plugin-solidtype/markers"] + + external children : t -> Jsx.element = "children" [@@mel.get] + end + + let make (props : Props.t) = + let (_ : palette) = in_palette "Command.Group" in + let counted, set_counted = S.create_signal [] in + let heading_id = S.create_unique_id () in + let heading_here, labelled = named heading_id in + let g = + { + count = + (fun shown -> + S.update set_counted (fun l -> shown :: l); + S.on_cleanup (fun () -> + S.update set_counted (Stdlib.List.filter (fun s -> s != shown)))); + heading_id; + heading_here; + } + in + (* Its items are made while it is hidden too, so they are filtered and + show it again. *) + let resolved = + S.children (fun () -> + + (Props.children props) + ) + in + +
+ (Jsx.fragment (S.to_array (S.get resolved))) +
+
+ + let component = Jsx.component make +end + +module Heading = struct + module Props = struct + type t + + external check : + ?as_:(Solidtype.Html.Header.props -> Solidtype.Jsx.element) -> + ?props:'spread Solidtype.Solid.rest -> + (?children:('f, [ `elements ]) Solidtype.Check.children -> unit -> unit) + Solidtype.Html.Header.check = "check" + [@@mel.module "vite-plugin-solidtype/markers"] + + external children : t -> Jsx.element = "children" [@@mel.get] + end + + let make (props : Props.t) = + let g = + match Group_context.use () with + | Some g -> g + | None -> invalid_arg "Command.Heading is used inside a Command.Group" + in + g.heading_here (); +
+ (Props.children props) +
+ + let component = Jsx.component make +end + +module Separator = struct + module Props = struct + type t + + external check : + ?as_:(Solidtype.Html.Div.props -> Solidtype.Jsx.element) -> + ?props:'spread Solidtype.Solid.rest -> + (unit -> unit) Solidtype.Html.Div.check = "check" + [@@mel.module "vite-plugin-solidtype/markers"] + + external ref : t -> ((Dom.htmlDivElement -> unit)[@u]) option = "ref" + [@@mel.get] + end + + let make (props : Props.t) = + let p = in_palette "Command.Separator" in + let element, set_element = S.create_signal None in + (* It stays on the page while hidden, where its place among the items + says whether it parts any. *) +
+ S.set set_element (Some e); + p.add_separator e; + S.on_cleanup (fun () -> p.drop_separator e); + Jsx.call (Props.ref props) e) + hidden=(Jsx.bool + (match S.get element with + | Some e -> + not (Array.exists (fun s -> s == e) (S.get p.parting)) + | None -> true)) + ?as_=(S.drawn_as props) + props=(S.undrawn (S.omit props [| "ref" |])) + /> + + let component = Jsx.component make +end diff --git a/headless/date_field.mli b/headless/date_field.mli index 4156989..e03e87a 100644 --- a/headless/date_field.mli +++ b/headless/date_field.mli @@ -109,8 +109,8 @@ module Context : sig end (** A field of a date, a [
]. Its value is [value] while it is given one -- - [Js.Nullable.null] keeps it empty -- else the one it keeps, [default_value] - at first, and each change calls [on_change], [None] once every segment is + [Js.Null.empty] keeps it empty -- else the one it keeps, [default_value] at + first, and each change calls [on_change], [None] once every segment is erased. A segment typed while it has none starts from [placeholder_value], else today. It is named by [aria_label] or [aria_labelledby], else by its {!Label}, and described by its {!Description} and {!Error_message}; @@ -125,7 +125,7 @@ module Root : sig type t val check : - ?value:D.Types.CalendarDate.t Js.Nullable.t Solidtype.Check.value -> + ?value:D.Types.CalendarDate.t Js.Null.t Solidtype.Check.value -> ?default_value:D.Types.CalendarDate.t Solidtype.Check.value -> ?on_change:(D.Types.CalendarDate.t option -> unit) -> ?placeholder_value:D.Types.CalendarDate.t Solidtype.Check.value -> @@ -164,7 +164,7 @@ module Root_date_time : sig type t val check : - ?value:D.Types.CalendarDateTime.t Js.Nullable.t Solidtype.Check.value -> + ?value:D.Types.CalendarDateTime.t Js.Null.t Solidtype.Check.value -> ?default_value:D.Types.CalendarDateTime.t Solidtype.Check.value -> ?on_change:(D.Types.CalendarDateTime.t option -> unit) -> ?placeholder_value:D.Types.CalendarDateTime.t Solidtype.Check.value -> @@ -207,7 +207,7 @@ module Root_zoned : sig type t val check : - ?value:D.Types.ZonedDateTime.t Js.Nullable.t Solidtype.Check.value -> + ?value:D.Types.ZonedDateTime.t Js.Null.t Solidtype.Check.value -> ?default_value:D.Types.ZonedDateTime.t Solidtype.Check.value -> ?on_change:(D.Types.ZonedDateTime.t option -> unit) -> placeholder_value:D.Types.ZonedDateTime.t Solidtype.Check.value -> @@ -247,13 +247,15 @@ end there, kept or given as a field's is; [on_change] is told it, and [None] as an end is erased. It is invalid while [is_invalid], while its end is before its start, and while either end is out of its range or unavailable; it is - described by the range as its locale tells it. *) + described by the range as its locale tells it. The arrows, Backspace in an + empty segment and a segment typed whole move through both ends' segments, as + React Aria's group does. *) module Root_range : sig module Props : sig type t val check : - ?value:D.Types.CalendarDate.t range Js.Nullable.t Solidtype.Check.value -> + ?value:D.Types.CalendarDate.t range Js.Null.t Solidtype.Check.value -> ?default_value:D.Types.CalendarDate.t range Solidtype.Check.value -> ?on_change:(D.Types.CalendarDate.t range option -> unit) -> ?start_name:string Solidtype.Check.value -> @@ -288,8 +290,7 @@ module Root_range_date_time : sig type t val check : - ?value: - D.Types.CalendarDateTime.t range Js.Nullable.t Solidtype.Check.value -> + ?value:D.Types.CalendarDateTime.t range Js.Null.t Solidtype.Check.value -> ?default_value:D.Types.CalendarDateTime.t range Solidtype.Check.value -> ?on_change:(D.Types.CalendarDateTime.t range option -> unit) -> ?start_name:string Solidtype.Check.value -> @@ -326,7 +327,7 @@ module Root_range_zoned : sig type t val check : - ?value:D.Types.ZonedDateTime.t range Js.Nullable.t Solidtype.Check.value -> + ?value:D.Types.ZonedDateTime.t range Js.Null.t Solidtype.Check.value -> ?default_value:D.Types.ZonedDateTime.t range Solidtype.Check.value -> ?on_change:(D.Types.ZonedDateTime.t range option -> unit) -> ?start_name:string Solidtype.Check.value -> diff --git a/headless/date_field.mlx b/headless/date_field.mlx index 9056666..abdbefb 100644 --- a/headless/date_field.mlx +++ b/headless/date_field.mlx @@ -1223,6 +1223,17 @@ module Slots_context = Solidtype.Context.Make_optional (struct type t = (string * inner) list end) +(* The inputs whose segments the keys move through: both of a range's, as + React Aria moves through its group, else the field's own. Read in a part's + setup, where its contexts are. *) +let scope (f : inner) = + let slots = Slots_context.use () in + fun () -> + match slots with + | Some slots when List.exists (fun (_, s) -> s == f) slots -> + List.filter_map (fun (_, (s : inner)) -> S.get s.input) slots + | Some _ | None -> Option.to_list (S.get f.input) + let within part = match Field_context.use () with | Some f -> f @@ -1242,18 +1253,22 @@ let named id = in (here, fun () -> if S.get count > 0 then Some id else None) -(* The segments Tab reaches, in the page's order. *) -let focusable input = - Js.Array.from (query_all input "[data-type][tabindex=\"0\"]") +(* The segments Tab reaches in [inputs], in the page's order. *) +let focusable inputs = + Array.concat + (List.map + (fun input -> + Js.Array.from (query_all input "[data-type][tabindex=\"0\"]")) + inputs) let focus_first input = - match focusable input with [||] -> () | all -> focus all.(0) + match focusable [ input ] with [||] -> () | all -> focus all.(0) let index_of all e = Js.Array.findIndex ~f:(fun s -> s == e) all (* The segment after the one focused, or before it. *) -let focus_by input amount = - let all = focusable input in +let focus_by inputs amount = + let all = focusable inputs in match Js.Null.toOption active with | Some a -> let at = index_of all a in @@ -1266,7 +1281,7 @@ let focus_by input amount = (* The segment nearest [from] on the side [sign] says, by where each is drawn, as React Aria moves through a right-to-left field. *) -let focus_beside input (from : Dom.element) sign = +let focus_beside inputs (from : Dom.element) sign = let x = rect_left (rect from) in let nearest = Array.fold_left @@ -1277,14 +1292,14 @@ let focus_beside input (from : Dom.element) sign = | Some (_, bd) when Float.abs bd <= Float.abs d -> best | Some _ | None -> Some (s, d) else best) - None (focusable input) + None (focusable inputs) in match nearest with Some (s, _) -> focus s | None -> () (* The first segment with no part of those that end the field, or its last, as a press on the field outside a segment focuses. *) let focus_last input = - let all = focusable input in + let all = focusable [ input ] in let n = Array.length all in if n > 0 then let rec back i = @@ -1345,7 +1360,7 @@ let format_options ~granularity ~time_zone ~hide_time_zone ~hour_cycle ~show_era () (* A field of any kind of value: what each root is, given its kind. *) -let field (type v) ~(kind : v kind) ~(value : v Js.Nullable.t option S.accessor) +let field (type v) ~(kind : v kind) ~(value : v Js.Null.t option S.accessor) ~(default_value : v option) ~(on_change : v option -> unit) ~(placeholder_value : v option S.accessor) ~(today : string -> Dt.Calendar.t -> v) ~(min_value : v option S.accessor) @@ -1419,7 +1434,7 @@ let field (type v) ~(kind : v kind) ~(value : v Js.Nullable.t option S.accessor) let current = S.create_memo (fun () -> match S.get value with - | Some v -> Js.Nullable.toOption v + | Some v -> Js.Null.toOption v | None -> S.get kept) in let set_value v = @@ -1938,7 +1953,7 @@ module Common = struct end (* What a root does of a value it is given: a value given, an empty one - [Js.Nullable.null], or none, the root keeping its own. *) + [Js.Null.empty], or none, the root keeping its own. *) let given value props = S.accessor (fun () -> value props) (* Two dates as a reader is told them, split at the last literal their @@ -1974,7 +1989,7 @@ let told_range format a b = them: each its end of the range, the range told once both are there and [None] as one is erased, invalid together, and described as a range. *) let range_field (type v) ~(kind : v kind) - ~(value : v range Js.Nullable.t option S.accessor) + ~(value : v range Js.Null.t option S.accessor) ~(default_value : v range option) ~(on_change : v range option -> unit) ~(placeholder_value : v option S.accessor) ~(today : string -> Dt.Calendar.t -> v) ~(min_value : v option S.accessor) @@ -2002,7 +2017,7 @@ let range_field (type v) ~(kind : v kind) S.create_memo (fun () -> match S.get value with | Some n -> ( - match Js.Nullable.toOption n with + match Js.Null.toOption n with | Some r -> (Some r.start, Some r.end_) | None -> ( (* A range controlled empty forgets one typed whole. *) @@ -2044,8 +2059,7 @@ let range_field (type v) ~(kind : v kind) let engine ~name ~label ~labelled_by ~described_by ~auto_focus ~pick ~put = field ~kind ~value: - (S.accessor (fun () -> - Some (Js.Nullable.fromOption (pick (S.get shown))))) + (S.accessor (fun () -> Some (Js.Null.fromOption (pick (S.get shown))))) ~default_value:None ~on_change:(fun v -> put v (S.untrack (fun () -> S.get shown))) ~placeholder_value ~today ~min_value ~max_value ~is_date_unavailable @@ -2145,7 +2159,7 @@ module Root = struct type t = Common.t external check : - ?value:V.t Js.Nullable.t Solidtype.Check.value -> + ?value:V.t Js.Null.t Solidtype.Check.value -> ?default_value:V.t Solidtype.Check.value -> ?on_change:(V.t option -> unit) -> ?placeholder_value:V.t Solidtype.Check.value -> @@ -2170,7 +2184,7 @@ module Root = struct Solidtype.Html.Div.check = "check" [@@mel.module "vite-plugin-solidtype/markers"] - external value : t -> V.t Js.Nullable.t option = "value" [@@mel.get] + external value : t -> V.t Js.Null.t option = "value" [@@mel.get] external default_value : t -> V.t option = "defaultValue" [@@mel.get] external on_change : t -> ((V.t option -> unit)[@u]) option = "onChange" @@ -2216,7 +2230,7 @@ module Root_date_time = struct type t = Common.t external check : - ?value:V.t Js.Nullable.t Solidtype.Check.value -> + ?value:V.t Js.Null.t Solidtype.Check.value -> ?default_value:V.t Solidtype.Check.value -> ?on_change:(V.t option -> unit) -> ?placeholder_value:V.t Solidtype.Check.value -> @@ -2243,7 +2257,7 @@ module Root_date_time = struct Solidtype.Html.Div.check = "check" [@@mel.module "vite-plugin-solidtype/markers"] - external value : t -> V.t Js.Nullable.t option = "value" [@@mel.get] + external value : t -> V.t Js.Null.t option = "value" [@@mel.get] external default_value : t -> V.t option = "defaultValue" [@@mel.get] external on_change : t -> ((V.t option -> unit)[@u]) option = "onChange" @@ -2292,7 +2306,7 @@ module Root_zoned = struct type t = Common.t external check : - ?value:V.t Js.Nullable.t Solidtype.Check.value -> + ?value:V.t Js.Null.t Solidtype.Check.value -> ?default_value:V.t Solidtype.Check.value -> ?on_change:(V.t option -> unit) -> placeholder_value:V.t Solidtype.Check.value -> @@ -2320,7 +2334,7 @@ module Root_zoned = struct Solidtype.Html.Div.check = "check" [@@mel.module "vite-plugin-solidtype/markers"] - external value : t -> V.t Js.Nullable.t option = "value" [@@mel.get] + external value : t -> V.t Js.Null.t option = "value" [@@mel.get] external default_value : t -> V.t option = "defaultValue" [@@mel.get] external on_change : t -> ((V.t option -> unit)[@u]) option = "onChange" @@ -2400,7 +2414,7 @@ module Root_range = struct type t = Common.t external check : - ?value:V.t range Js.Nullable.t Solidtype.Check.value -> + ?value:V.t range Js.Null.t Solidtype.Check.value -> ?default_value:V.t range Solidtype.Check.value -> ?on_change:(V.t range option -> unit) -> ?start_name:string Solidtype.Check.value -> @@ -2424,7 +2438,7 @@ module Root_range = struct Solidtype.Html.Div.check = "check" [@@mel.module "vite-plugin-solidtype/markers"] - external value : t -> V.t range Js.Nullable.t option = "value" [@@mel.get] + external value : t -> V.t range Js.Null.t option = "value" [@@mel.get] external default_value : t -> V.t range option = "defaultValue" [@@mel.get] external on_change : t -> ((V.t range option -> unit)[@u]) option @@ -2473,7 +2487,7 @@ module Root_range_date_time = struct type t = Common.t external check : - ?value:V.t range Js.Nullable.t Solidtype.Check.value -> + ?value:V.t range Js.Null.t Solidtype.Check.value -> ?default_value:V.t range Solidtype.Check.value -> ?on_change:(V.t range option -> unit) -> ?start_name:string Solidtype.Check.value -> @@ -2499,7 +2513,7 @@ module Root_range_date_time = struct Solidtype.Html.Div.check = "check" [@@mel.module "vite-plugin-solidtype/markers"] - external value : t -> V.t range Js.Nullable.t option = "value" [@@mel.get] + external value : t -> V.t range Js.Null.t option = "value" [@@mel.get] external default_value : t -> V.t range option = "defaultValue" [@@mel.get] external on_change : t -> ((V.t range option -> unit)[@u]) option @@ -2551,7 +2565,7 @@ module Root_range_zoned = struct type t = Common.t external check : - ?value:V.t range Js.Nullable.t Solidtype.Check.value -> + ?value:V.t range Js.Null.t Solidtype.Check.value -> ?default_value:V.t range Solidtype.Check.value -> ?on_change:(V.t range option -> unit) -> ?start_name:string Solidtype.Check.value -> @@ -2578,7 +2592,7 @@ module Root_range_zoned = struct Solidtype.Html.Div.check = "check" [@@mel.module "vite-plugin-solidtype/markers"] - external value : t -> V.t range Js.Nullable.t option = "value" [@@mel.get] + external value : t -> V.t range Js.Null.t option = "value" [@@mel.get] external default_value : t -> V.t range option = "defaultValue" [@@mel.get] external on_change : t -> ((V.t range option -> unit)[@u]) option @@ -2719,15 +2733,16 @@ module Input = struct match slotted with Some f -> f | None -> within "Date_field.Input" in let element = ref None in + let inputs = scope f in let move (e : Dom.keyboardEvent) ~right = - match !element with - | None -> () - | Some input -> ( + match inputs () with + | [] -> () + | inputs -> ( prevent_key e; match S.get f.direction with - | `ltr -> ignore (focus_by input (if right then 1 else -1) : bool) + | `ltr -> ignore (focus_by inputs (if right then 1 else -1) : bool) | `rtl -> - focus_beside input (key_target e) (if right then 1. else -1.)) + focus_beside inputs (key_target e) (if right then 1. else -1.)) in let pressed_by_touch = ref false in @@ -2887,6 +2902,7 @@ module Segment = struct let make (props : Props.t) = let f = within "Date_field.Segment" in + let inputs = scope f in let entered = ref "" in let element = ref None in let composition = ref "" in @@ -2976,12 +2992,8 @@ module Segment = struct [||]) in let starts_with name typed = K.Filter.starts_with filter name typed in - let next () = - Option.iter (fun i -> ignore (focus_by i 1 : bool)) (S.get f.input) - in - let previous () = - Option.iter (fun i -> ignore (focus_by i (-1) : bool)) (S.get f.input) - in + let next () = ignore (focus_by (inputs ()) 1 : bool) in + let previous () = ignore (focus_by (inputs ()) (-1) : bool) in let erase () = let s = seg () in if String.equal s.text s.placeholder then previous (); @@ -3147,7 +3159,7 @@ module Segment = struct before it, or after it. *) match (S.get f.input, Js.Null.toOption active) with | Some input, Some a when a == e -> - let all = focusable input in + let all = focusable [ input ] in let at = index_of all e in let rest = Js.Array.filter ~f:(fun s -> s != e) all in let target = if at > 0 then at - 1 else 0 in diff --git a/headless/date_picker.mli b/headless/date_picker.mli index d69dc5d..c6e5bad 100644 --- a/headless/date_picker.mli +++ b/headless/date_picker.mli @@ -85,7 +85,7 @@ module Root : sig type t val check : - ?value:D.Types.CalendarDate.t Js.Nullable.t Solidtype.Check.value -> + ?value:D.Types.CalendarDate.t Js.Null.t Solidtype.Check.value -> ?default_value:D.Types.CalendarDate.t Solidtype.Check.value -> ?on_change:(D.Types.CalendarDate.t option -> unit) -> ?placeholder_value:D.Types.CalendarDate.t Solidtype.Check.value -> @@ -133,7 +133,7 @@ module Root_date_time : sig type t val check : - ?value:D.Types.CalendarDateTime.t Js.Nullable.t Solidtype.Check.value -> + ?value:D.Types.CalendarDateTime.t Js.Null.t Solidtype.Check.value -> ?default_value:D.Types.CalendarDateTime.t Solidtype.Check.value -> ?on_change:(D.Types.CalendarDateTime.t option -> unit) -> ?placeholder_value:D.Types.CalendarDateTime.t Solidtype.Check.value -> @@ -183,7 +183,7 @@ module Root_zoned : sig type t val check : - ?value:D.Types.ZonedDateTime.t Js.Nullable.t Solidtype.Check.value -> + ?value:D.Types.ZonedDateTime.t Js.Null.t Solidtype.Check.value -> ?default_value:D.Types.ZonedDateTime.t Solidtype.Check.value -> ?on_change:(D.Types.ZonedDateTime.t option -> unit) -> placeholder_value:D.Types.ZonedDateTime.t Solidtype.Check.value -> @@ -252,8 +252,7 @@ module Root_range : sig val check : ?value: - D.Types.CalendarDate.t Date_field.range Js.Nullable.t - Solidtype.Check.value -> + D.Types.CalendarDate.t Date_field.range Js.Null.t Solidtype.Check.value -> ?default_value: D.Types.CalendarDate.t Date_field.range Solidtype.Check.value -> ?on_change:(D.Types.CalendarDate.t Date_field.range option -> unit) -> @@ -305,7 +304,7 @@ module Root_range_date_time : sig val check : ?value: - D.Types.CalendarDateTime.t Date_field.range Js.Nullable.t + D.Types.CalendarDateTime.t Date_field.range Js.Null.t Solidtype.Check.value -> ?default_value: D.Types.CalendarDateTime.t Date_field.range Solidtype.Check.value -> @@ -358,8 +357,7 @@ module Root_range_zoned : sig val check : ?value: - D.Types.ZonedDateTime.t Date_field.range Js.Nullable.t - Solidtype.Check.value -> + D.Types.ZonedDateTime.t Date_field.range Js.Null.t Solidtype.Check.value -> ?default_value: D.Types.ZonedDateTime.t Date_field.range Solidtype.Check.value -> ?on_change:(D.Types.ZonedDateTime.t Date_field.range option -> unit) -> diff --git a/headless/date_picker.mlx b/headless/date_picker.mlx index da4f99d..30a9ce6 100644 --- a/headless/date_picker.mlx +++ b/headless/date_picker.mlx @@ -260,9 +260,9 @@ let opened (props : Common.t) = value, the date and the time chosen apart while it has none, and whether its popover is open. [field] draws the root's date field, given its value, what it is told and what it holds. *) -let picker (type v) ~(kind : v kind) - ~(value : v Js.Nullable.t option S.accessor) ~(default_value : v option) - ~(on_change : v option -> unit) ~(placeholder_value : v option S.accessor) +let picker (type v) ~(kind : v kind) ~(value : v Js.Null.t option S.accessor) + ~(default_value : v option) ~(on_change : v option -> unit) + ~(placeholder_value : v option S.accessor) ~(min_value : v option S.accessor) ~(max_value : v option S.accessor) ~(is_date_unavailable : (v -> bool) option S.accessor) ~(children : unit -> v state Jsx.children) @@ -270,16 +270,14 @@ let picker (type v) ~(kind : v kind) v state -> (Solidtype.Html.Div.props -> Jsx.element) option S.accessor) (props : Common.t) ~(field : - value:v Js.Nullable.t S.accessor -> + value:v Js.Null.t S.accessor -> on_change:(v option -> unit) -> as_:(Solidtype.Html.Div.props -> Jsx.element) option S.accessor -> (unit -> Jsx.element) -> Jsx.element) = let kept, set_kept = S.create_signal default_value in let current () = - match S.get value with - | Some v -> Js.Nullable.toOption v - | None -> S.get kept + match S.get value with Some v -> Js.Null.toOption v | None -> S.get kept in let set_value v = if Option.is_none (S.get value) then S.set set_kept v; @@ -391,7 +389,7 @@ let picker (type v) ~(kind : v kind) in shell ~is_open ~set_open props (fun () -> field - ~value:(S.accessor (fun () -> Js.Nullable.fromOption (current ()))) + ~value:(S.accessor (fun () -> Js.Null.fromOption (current ()))) ~on_change:set_value ~as_:(as_ state) inside) module Root = struct @@ -402,7 +400,7 @@ module Root = struct type predicate external check : - ?value:V.t Js.Nullable.t Solidtype.Check.value -> + ?value:V.t Js.Null.t Solidtype.Check.value -> ?default_value:V.t Solidtype.Check.value -> ?on_change:(V.t option -> unit) -> ?placeholder_value:V.t Solidtype.Check.value -> @@ -436,7 +434,7 @@ module Root = struct (unit -> unit) Solidtype.Html.Div.check = "check" [@@mel.module "vite-plugin-solidtype/markers"] - external value : t -> V.t Js.Nullable.t option = "value" [@@mel.get] + external value : t -> V.t Js.Null.t option = "value" [@@mel.get] external default_value : t -> V.t option = "defaultValue" [@@mel.get] external on_change : t -> ((V.t option -> unit)[@u]) option = "onChange" @@ -512,7 +510,7 @@ module Root_date_time = struct type predicate external check : - ?value:V.t Js.Nullable.t Solidtype.Check.value -> + ?value:V.t Js.Null.t Solidtype.Check.value -> ?default_value:V.t Solidtype.Check.value -> ?on_change:(V.t option -> unit) -> ?placeholder_value:V.t Solidtype.Check.value -> @@ -548,7 +546,7 @@ module Root_date_time = struct (unit -> unit) Solidtype.Html.Div.check = "check" [@@mel.module "vite-plugin-solidtype/markers"] - external value : t -> V.t Js.Nullable.t option = "value" [@@mel.get] + external value : t -> V.t Js.Null.t option = "value" [@@mel.get] external default_value : t -> V.t option = "defaultValue" [@@mel.get] external on_change : t -> ((V.t option -> unit)[@u]) option = "onChange" @@ -636,7 +634,7 @@ module Root_zoned = struct type predicate external check : - ?value:V.t Js.Nullable.t Solidtype.Check.value -> + ?value:V.t Js.Null.t Solidtype.Check.value -> ?default_value:V.t Solidtype.Check.value -> ?on_change:(V.t option -> unit) -> placeholder_value:V.t Solidtype.Check.value -> @@ -673,7 +671,7 @@ module Root_zoned = struct (unit -> unit) Solidtype.Html.Div.check = "check" [@@mel.module "vite-plugin-solidtype/markers"] - external value : t -> V.t Js.Nullable.t option = "value" [@@mel.get] + external value : t -> V.t Js.Null.t option = "value" [@@mel.get] external default_value : t -> V.t option = "defaultValue" [@@mel.get] external on_change : t -> ((V.t option -> unit)[@u]) option = "onChange" @@ -778,7 +776,7 @@ type 'v range_state = { range, the dates and the times chosen apart while it has none, and whether its popover is open. *) let range_picker (type v) ~(kind : v kind) - ~(value : v Date_field.range Js.Nullable.t option S.accessor) + ~(value : v Date_field.range Js.Null.t option S.accessor) ~(default_value : v Date_field.range option) ~(on_change : v Date_field.range option -> unit) ~(placeholder_value : v option S.accessor) @@ -790,16 +788,14 @@ let range_picker (type v) ~(kind : v kind) (Solidtype.Html.Div.props -> Jsx.element) option S.accessor) (props : Common.t) ~(field : - value:v Date_field.range Js.Nullable.t S.accessor -> + value:v Date_field.range Js.Null.t S.accessor -> on_change:(v Date_field.range option -> unit) -> as_:(Solidtype.Html.Div.props -> Jsx.element) option S.accessor -> (unit -> Jsx.element) -> Jsx.element) = let kept, set_kept = S.create_signal default_value in let current () = - match S.get value with - | Some v -> Js.Nullable.toOption v - | None -> S.get kept + match S.get value with Some v -> Js.Null.toOption v | None -> S.get kept in let set_value v = if Option.is_none (S.get value) then S.set set_kept v; @@ -926,7 +922,7 @@ let range_picker (type v) ~(kind : v kind) in shell ~is_open ~set_open props (fun () -> field - ~value:(S.accessor (fun () -> Js.Nullable.fromOption (current ()))) + ~value:(S.accessor (fun () -> Js.Null.fromOption (current ()))) ~on_change:set_value ~as_:(as_ state) inside) module Root_range = struct @@ -937,7 +933,7 @@ module Root_range = struct type predicate external check : - ?value:V.t Date_field.range Js.Nullable.t Solidtype.Check.value -> + ?value:V.t Date_field.range Js.Null.t Solidtype.Check.value -> ?default_value:V.t Date_field.range Solidtype.Check.value -> ?on_change:(V.t Date_field.range option -> unit) -> ?start_name:string Solidtype.Check.value -> @@ -972,7 +968,7 @@ module Root_range = struct (unit -> unit) Solidtype.Html.Div.check = "check" [@@mel.module "vite-plugin-solidtype/markers"] - external value : t -> V.t Date_field.range Js.Nullable.t option = "value" + external value : t -> V.t Date_field.range Js.Null.t option = "value" [@@mel.get] external default_value : t -> V.t Date_field.range option = "defaultValue" @@ -1052,7 +1048,7 @@ module Root_range_date_time = struct type predicate external check : - ?value:V.t Date_field.range Js.Nullable.t Solidtype.Check.value -> + ?value:V.t Date_field.range Js.Null.t Solidtype.Check.value -> ?default_value:V.t Date_field.range Solidtype.Check.value -> ?on_change:(V.t Date_field.range option -> unit) -> ?start_name:string Solidtype.Check.value -> @@ -1089,7 +1085,7 @@ module Root_range_date_time = struct (unit -> unit) Solidtype.Html.Div.check = "check" [@@mel.module "vite-plugin-solidtype/markers"] - external value : t -> V.t Date_field.range Js.Nullable.t option = "value" + external value : t -> V.t Date_field.range Js.Null.t option = "value" [@@mel.get] external default_value : t -> V.t Date_field.range option = "defaultValue" @@ -1181,7 +1177,7 @@ module Root_range_zoned = struct type predicate external check : - ?value:V.t Date_field.range Js.Nullable.t Solidtype.Check.value -> + ?value:V.t Date_field.range Js.Null.t Solidtype.Check.value -> ?default_value:V.t Date_field.range Solidtype.Check.value -> ?on_change:(V.t Date_field.range option -> unit) -> ?start_name:string Solidtype.Check.value -> @@ -1219,7 +1215,7 @@ module Root_range_zoned = struct (unit -> unit) Solidtype.Html.Div.check = "check" [@@mel.module "vite-plugin-solidtype/markers"] - external value : t -> V.t Date_field.range Js.Nullable.t option = "value" + external value : t -> V.t Date_field.range Js.Null.t option = "value" [@@mel.get] external default_value : t -> V.t Date_field.range option = "defaultValue" diff --git a/headless/number_field.mli b/headless/number_field.mli index 6c7bfd5..698a7b3 100644 --- a/headless/number_field.mli +++ b/headless/number_field.mli @@ -6,7 +6,8 @@ and turned up steps it down, as React Aria's does, where Kobalte's goes the other way; and it leaves alone a field that is read-only, a turn mostly sideways, and one with Control held, which zooms the page, where Kobalte's - steps on each. + steps on each. A [raw_value] of [nan] empties it, as React Aria's, where + Kobalte's keeps the last number shown. {[ diff --git a/headless/number_field.mlx b/headless/number_field.mlx index 3698ab9..0a8df26 100644 --- a/headless/number_field.mlx +++ b/headless/number_field.mlx @@ -36,6 +36,28 @@ module Wheel = Solidtype.Context.Make_optional (struct type t = bool S.accessor end) +(* A raw value that is not a number empties the field, as React Aria's does, + where Kobalte's leaves the last number shown. *) +module Emptied = struct + let%component make ?(raw_value : float option) () = + let field = Kobalte.use_number_field_context () in + S.create_effect + (S.on ~defer:true + (fun () -> S.get raw_value) + (fun raw _ -> + match raw with + | Some r + when Float.is_nan r + && not (Float.is_nan (S.get (Field_context.raw_value field))) + -> + S.batch (fun () -> + Field_context.set_value field + (Solidtype_kobalte.Types.Either.left ""); + Field_context.format field ()) + | Some _ | None -> ())); + Jsx.nothing +end + module Root = struct module Props = struct include Kobalte.Root.Props @@ -43,6 +65,8 @@ module Root = struct type t external change_on_wheel : t -> bool option = "changeOnWheel" [@@mel.get] + external raw_value : t -> float option = "rawValue" [@@mel.get] + external children : t -> Jsx.element = "children" [@@mel.get] end let make (props : Props.t) = @@ -52,8 +76,10 @@ module Root = struct in + change_on_wheel=false + props=(S.omit props [| "changeOnWheel"; "children" |])> + (Props.children props) + let component = Jsx.component make diff --git a/heroui/autocomplete.mlx b/heroui/autocomplete.mlx index 05673ae..259a6b6 100644 --- a/heroui/autocomplete.mlx +++ b/heroui/autocomplete.mlx @@ -839,11 +839,9 @@ let%component make ?(placeholder : string = "Select an item") option_text_value=List_box.text option_label=List_box.text option_disabled=(fun o -> disabled_key (List_box.key o)) - value=(* [null], an empty selection Kobalte keeps: [undefined] - would be no value given. *) - (match S.get selected with - | o :: _ -> Js.Nullable.return o - | [] -> Js.Nullable.null) + value=(match S.get selected with + | o :: _ -> Js.Null.return o + | [] -> Js.Null.empty) on_change=(fun o -> change (match Js.Nullable.toOption o with diff --git a/heroui/calendar.mli b/heroui/calendar.mli index 8e4a684..976e838 100644 --- a/heroui/calendar.mli +++ b/heroui/calendar.mli @@ -57,7 +57,7 @@ module Props : sig type t val check : - ?value:D.Types.CalendarDate.t Js.Nullable.t Solidtype.Check.value -> + ?value:D.Types.CalendarDate.t Js.Null.t Solidtype.Check.value -> ?default_value:D.Types.CalendarDate.t Solidtype.Check.value -> ?on_change:(D.Types.CalendarDate.t -> unit) -> ?focused_value:D.Types.CalendarDate.t Solidtype.Check.value -> @@ -148,7 +148,7 @@ module Range : sig type t val check : - ?value:range Js.Nullable.t Solidtype.Check.value -> + ?value:range Js.Null.t Solidtype.Check.value -> ?default_value:range Solidtype.Check.value -> ?on_change:(range option -> unit) -> ?allows_non_contiguous_ranges:bool Solidtype.Check.value -> diff --git a/heroui/calendar.mlx b/heroui/calendar.mlx index 44b94c0..2d3ba64 100644 --- a/heroui/calendar.mlx +++ b/heroui/calendar.mlx @@ -239,9 +239,9 @@ let around () = around_unavailable = (fun () -> None); } -let%component make ?(value : C.t Js.Nullable.t option) - ?(default_value : C.t option) ?(on_change : (C.t -> unit) option) - ?(focused_value : C.t option) ?(default_focused_value : C.t option) +let%component make ?(value : C.t Js.Null.t option) ?(default_value : C.t option) + ?(on_change : (C.t -> unit) option) ?(focused_value : C.t option) + ?(default_focused_value : C.t option) ?(on_focus_change : (C.t -> unit) option) ?(min_value : C.t option) ?(max_value : C.t option) ?is_date_unavailable:(is_date_unavailable_given : (C.t -> bool) option) @@ -287,7 +287,7 @@ let%component make ?(value : C.t Js.Nullable.t option) Field.controlled ~value:(fun () -> match (S.get value, picked) with - | Some v, _ -> Some (Js.Nullable.toOption v) + | Some v, _ -> Some (Js.Null.toOption v) | None, Some v -> Some (S.get v) | None, None -> None) ~default:(S.get default_value) @@ -319,7 +319,7 @@ let%component make ?(value : C.t Js.Nullable.t option) in let state = { is_disabled; is_invalid = invalid } in set (Some d)) ?focused_value=(S.get focused_value) ?default_focused_value=(S.get default_focused_value) @@ -442,7 +442,7 @@ end type range = Headless.range = { start : C.t; end_ : C.t } module Range = struct - let%component make ?(value : range Js.Nullable.t option) + let%component make ?(value : range Js.Null.t option) ?(default_value : range option) ?(on_change : (range option -> unit) option) ?(allows_non_contiguous_ranges : bool option) @@ -495,7 +495,7 @@ module Range = struct Field.controlled ~value:(fun () -> match (S.get value, picked) with - | Some v, _ -> Some (Js.Nullable.toOption v) + | Some v, _ -> Some (Js.Null.toOption v) | None, Some v -> Some (S.get v) | None, None -> None) ~default:(S.get default_value) @@ -530,7 +530,7 @@ module Range = struct in let state = { is_disabled; is_invalid = invalid } in + ?value:Solidtype_kobalte.Types.Color.t Js.Null.t Solidtype.Check.value -> ?default_value:Solidtype_kobalte.Types.Color.t Solidtype.Check.value -> ?on_change:(Solidtype_kobalte.Types.Color.t option -> unit) -> ?channel:channel Solidtype.Check.value -> diff --git a/heroui/color_field.mlx b/heroui/color_field.mlx index b349a8c..00b7bc9 100644 --- a/heroui/color_field.mlx +++ b/heroui/color_field.mlx @@ -227,7 +227,7 @@ type state = { is_required : bool Solidtype.Solid.accessor; } -let%component make ?(value : Color.t Js.Nullable.t option) +let%component make ?(value : Color.t Js.Null.t option) ?(default_value : Color.t option) ?(on_change : (Color.t option -> unit) option) ?(channel : channel option) ?(color_space : [ `rgb | `hsl | `hsb ] option) ?(full_width : bool option) diff --git a/heroui/combo_box.mlx b/heroui/combo_box.mlx index c408287..537dbe8 100644 --- a/heroui/combo_box.mlx +++ b/heroui/combo_box.mlx @@ -609,11 +609,9 @@ let%component make ?(selection_mode : [ `single | `multiple ] = `single) option_text_value=List_box.text option_label=List_box.text option_disabled=(fun o -> disabled_key (List_box.key o)) - value=(* [null], an empty selection Kobalte keeps: [undefined] - would be no value given. *) - (match S.get selected with - | o :: _ -> Js.Nullable.return o - | [] -> Js.Nullable.null) + value=(match S.get selected with + | o :: _ -> Js.Null.return o + | [] -> Js.Null.empty) on_change=(fun o -> change (match Js.Nullable.toOption o with diff --git a/heroui/command.css b/heroui/command.css new file mode 100644 index 0000000..df6dddf --- /dev/null +++ b/heroui/command.css @@ -0,0 +1,155 @@ +/* Command, in HeroUI's look, which ships no stylesheet for it: Tailwind on + HeroUI's tokens, written as HeroUI writes its components' CSS, so an + application imports it into the components layer and restyles a part by a + rule for its class. */ + +.command__backdrop { + @apply fixed inset-0 z-(--z-index-overlay) flex h-(--visual-viewport-height) w-full items-start justify-center; + + &[data-entering="true"] { + @apply animate-in duration-150 ease-out fade-in-0; + } + + &[data-exiting="true"] { + @apply animate-out duration-100 ease-out fade-out-0; + } + + &[data-entering="true"], + &[data-exiting="true"] { + @apply will-change-[opacity] motion-reduce:animate-none; + } +} + +.command__backdrop--transparent { + @apply bg-transparent; +} + +.command__backdrop--opaque { + @apply bg-backdrop; +} + +.command__backdrop--blur { + @apply bg-backdrop backdrop-blur-md; +} + +.command__container { + @apply pointer-events-none flex h-(--visual-viewport-height) w-full items-start justify-center px-4 pt-[15vh]; + + &[data-entering="true"] { + @apply animate-in duration-200 ease-out-quad fade-in-0 zoom-in-95 slide-in-from-top-2; + } + + &[data-exiting="true"] { + @apply animate-out duration-100 ease-out-quad fade-out-0 zoom-out-95; + } + + &[data-entering="true"], + &[data-exiting="true"] { + @apply will-change-[opacity,transform] motion-reduce:animate-none; + } +} + +.command__dialog { + @apply pointer-events-auto relative flex w-full flex-col overflow-hidden rounded-2xl bg-overlay shadow-overlay outline-none; + interpolate-size: allow-keywords; + transition: height 200ms cubic-bezier(0.16, 1, 0.3, 1); + @apply motion-reduce:transition-none; +} + +.command__dialog--sm { + @apply max-h-[300px] max-w-sm; +} + +.command__dialog--md { + @apply max-h-[356px] max-w-lg; +} + +.command__dialog--lg { + @apply max-h-[440px] max-w-xl; +} + +.command__header { + @apply flex items-center gap-2 px-2 pt-2 text-sm; +} + +.command__input-group { + @apply flex items-center gap-2 border-b border-border/70; + + &[data-empty="true"] .command__input-group-clear-button { + @apply pointer-events-none opacity-0; + } +} + +.command__input-group-prefix { + @apply flex shrink-0 items-center ps-4 text-muted; +} + +.command__input-group-input { + @apply min-w-0 flex-1 bg-transparent py-3 text-sm text-foreground outline-none placeholder:text-muted; + + &:first-child { + @apply ps-4; + } + + &::-webkit-search-cancel-button, + &::-webkit-search-decoration { + @apply appearance-none; + } +} + +.command__input-group-clear-button { + @apply me-3 size-5 shrink-0 p-1; + + &:last-child { + @apply me-4; + } +} + +.command__input-group-suffix { + @apply flex shrink-0 items-center pe-4 text-muted; +} + +.command__list { + @apply flex-1 overflow-y-auto overscroll-contain p-1.5 outline-none; +} + +.command__group + .command__group { + @apply mt-1; +} + +.command__group-heading { + @apply px-3 py-1.5 text-xs font-medium text-muted; +} + +.command__item { + @apply relative flex cursor-(--cursor-interactive) items-center gap-2.5 rounded-lg px-3 py-2 text-sm text-foreground outline-none select-none; + + & > svg { + @apply size-4 shrink-0 text-muted; + } + + &[data-hovered="true"], + &[data-focused="true"] { + @apply bg-default; + } + + &[data-pressed="true"] { + @apply bg-default-hover; + } + + &[data-disabled="true"] { + @apply cursor-default opacity-(--disabled-opacity); + } +} + +.command__separator { + @apply mx-3 my-1.5 h-px bg-separator; +} + +.command__footer { + @apply flex items-center gap-2 border-t border-border/50 bg-default/50 px-3 py-1 text-xs text-muted; +} + +.command__empty { + @apply py-6 text-center text-sm text-muted; +} diff --git a/heroui/command.mli b/heroui/command.mli new file mode 100644 index 0000000..f28091d --- /dev/null +++ b/heroui/command.mli @@ -0,0 +1,387 @@ +(** Command: a command palette in HeroUI's look, a search that filters a menu of + commands in a dialog, on {!Solidtype_headless.Command} and Kobalte's + [Dialog]; its look is [command.css]'s, in the components layer. + + Its page, with its examples: + [$(opam var solidtype:doc)/pages/heroui/command.md]. + + {[ + + + + + + + + + + + + + run key)> + + "Create new file..." + + + + + + + ]} + + A {!Button} inside a palette is its trigger, and shows pressed while the + palette is open. The search takes the focus as the palette opens and keeps + it: Down and Up move through the items shown, wrapping at either end, Home + and End reach the first and the last, and Enter runs the item focused, as a + press does; the pointer focuses the item it is over. Typing shows the items + whose text holds the search's and focuses the first. Escape empties the + search, and once it is empty closes the palette, as a press on the backdrop + does, unless the backdrop says otherwise; the focus goes back to what had + it. A palette stays open as an item runs: its [on_action] closes it where it + should. + + A {!Backdrop} with no palette around it is a palette of its own, open as its + [is_open] says. It is drawn in a portal at the end of the page while it is + open, and while its exit animation plays after. + + A part but the backdrop is used inside a [Command.Backdrop], and a part of + the search or the list inside a {!Dialog}: elsewhere it raises + [Invalid_argument] as it is made. *) + +module S := Solidtype.Solid +module Jsx := Solidtype.Jsx + +(** What a part inside a {!Dialog} reads of the palette: the search's text and + how to set it, the item focused, whether the list shows none. *) +module Context : sig + val use : unit -> Solidtype_headless.Command.state + (** The palette above; outside a {!Dialog} it raises [Invalid_argument]. *) +end + +(** What covers the page while the palette is open: a [
], [opaque] by + default. A press on it closes the palette while [is_dismissable], and Escape + in an empty search while not [is_keyboard_dismiss_disabled]. With no palette + around it, [is_open], [default_open] and [on_open_change] are its own. It is + drawn at the end of the page's body, or inside [portal_container] while that + is given. *) +module Backdrop : sig + module Props : sig + type t + + val check : + ?variant:[ `opaque | `blur | `transparent ] Solidtype.Check.value -> + ?is_dismissable:bool Solidtype.Check.value -> + ?is_keyboard_dismiss_disabled:bool Solidtype.Check.value -> + ?is_open:bool Solidtype.Check.value -> + ?default_open:bool Solidtype.Check.value -> + ?on_open_change:(bool -> unit) -> + ?portal_container:_ Dom.element_like Solidtype.Check.value -> + ?class_:string Solidtype.Check.value -> + ?style:string Solidtype.Check.value -> + ?ref:(Dom.htmlDivElement -> unit) -> + children: + ( Dialog_frame.overlay_state -> Jsx.element, + [ `elements | `render ] ) + Solidtype.Check.children -> + ?as_: + (Solidtype.Html.Div.props -> Dialog_frame.overlay_state -> Jsx.element) -> + ?props:t S.rest -> + (unit -> unit) Solidtype.Html.Div.check + end + + val make : Props.t -> Jsx.element + val component : Props.t Jsx.component +end + +(** Where the palette is placed: a [
] that holds it near the top of the + page, its [size] [`md] by default. *) +module Container : sig + module Props : sig + type t + + val check : + ?size:[ `sm | `md | `lg ] Solidtype.Check.value -> + ?class_:string Solidtype.Check.value -> + ?ref:(Dom.htmlDivElement -> unit) -> + children: + ( Dialog_frame.overlay_state -> Jsx.element, + [ `elements | `render ] ) + Solidtype.Check.children -> + ?as_: + (Solidtype.Html.Div.props -> Dialog_frame.overlay_state -> Jsx.element) -> + ?props:t S.rest -> + (unit -> unit) Solidtype.Html.Div.check + end + + val make : Props.t -> Jsx.element + val component : Props.t Jsx.component +end + +(** The palette: a dialog, a [
] named [Command palette] unless it is + given a name, which grows and shrinks with what it shows. Its search's text + is [input_value] while it is given, else the text it keeps, + [default_input_value] at first; a change calls [on_input_change]. An item is + shown while [filter] keeps it, given its text and the search's, else while + its text holds the search's, whatever their case or accents. *) +module Dialog : sig + module Props : sig + type t + + val check : + ?aria_label:string Solidtype.Check.value -> + ?input_value:string Solidtype.Check.value -> + ?default_input_value:string Solidtype.Check.value -> + ?on_input_change:(string -> unit) -> + ?filter:(string -> string -> bool) -> + ?class_:string Solidtype.Check.value -> + ?ref:(Dom.htmlElement -> unit) -> + ?on_focus_out:(Dom.focusEvent -> unit) -> + children: + ( Dialog_frame.dialog_state -> Jsx.element, + [ `elements | `render ] ) + Solidtype.Check.children -> + ?as_: + (Solidtype.Html.Section.props -> + Dialog_frame.dialog_state -> + Jsx.element) -> + ?props:t S.rest -> + (unit -> unit) Solidtype.Html.Section.check + end + + val make : Props.t -> Jsx.element + val component : Props.t Jsx.component +end + +(** What sits above the search, a breadcrumb or tabs: a [
]. *) +module Header : sig + module Props : sig + type t + + val check : + ?class_:string Solidtype.Check.value -> + children:(_, [ `elements ]) Solidtype.Check.children -> + ?as_:(Solidtype.Html.Div.props -> Jsx.element) -> + ?props:t S.rest -> + (unit -> unit) Solidtype.Html.Div.check + end + + val make : Props.t -> Jsx.element + val component : Props.t Jsx.component +end + +(** The search's row: a [
] of its {!Input_group.Prefix}, + {!Input_group.Input}, {!Input_group.Clear_button} and {!Input_group.Suffix}, + [data-empty] while the search is. Its input takes the focus as the palette + opens, while [auto_focus], the default. *) +module Input_group : sig + (** What leads the search, its icon: a [
]. *) + module Prefix : sig + module Props : sig + type t + + val check : + ?class_:string Solidtype.Check.value -> + children:(_, [ `elements ]) Solidtype.Check.children -> + ?as_:(Solidtype.Html.Div.props -> Jsx.element) -> + ?props:t S.rest -> + (unit -> unit) Solidtype.Html.Div.check + end + + val make : Props.t -> Jsx.element + val component : Props.t Jsx.component + end + + (** The search: an [] named [Search commands] unless it + is given a name, its [placeholder] [Search commands...] unless it is given + one. *) + module Input : sig + module Props : sig + type t + + val check : + ?placeholder:string Solidtype.Check.value -> + ?aria_label:string Solidtype.Check.value -> + ?class_:string Solidtype.Check.value -> + ?ref:(Dom.htmlInputElement -> unit) -> + ?as_:(Solidtype.Html.Input.props -> Jsx.element) -> + ?props:t S.rest -> + (unit -> unit) Solidtype.Html.Input.check + end + + val make : Props.t -> Jsx.element + val component : Props.t Jsx.component + end + + (** HeroUI's close button, named [Clear search] in the reader's language, + which empties the search and gives it the focus back; hidden while the + search is empty, and passed by Tab. *) + module Clear_button : sig + module Props : sig + type t + + val check : + ?class_:string Solidtype.Check.value -> + ?on_click:(Dom.mouseEvent -> unit) -> + ?as_:(Solidtype.Html.Button.props -> Jsx.element) -> + ?props:t S.rest -> + (unit -> unit) Solidtype.Html.Button.check + end + + val make : Props.t -> Jsx.element + val component : Props.t Jsx.component + end + + (** What ends the search, a key's hint: a [
]. *) + module Suffix : sig + module Props : sig + type t + + val check : + ?class_:string Solidtype.Check.value -> + children:(_, [ `elements ]) Solidtype.Check.children -> + ?as_:(Solidtype.Html.Div.props -> Jsx.element) -> + ?props:t S.rest -> + (unit -> unit) Solidtype.Html.Div.check + end + + val make : Props.t -> Jsx.element + val component : Props.t Jsx.component + end + + module Props : sig + type t + + val check : + ?auto_focus:bool Solidtype.Check.value -> + ?class_:string Solidtype.Check.value -> + children:(_, [ `elements ]) Solidtype.Check.children -> + ?as_:(Solidtype.Html.Div.props -> Jsx.element) -> + ?props:t S.rest -> + (unit -> unit) Solidtype.Html.Div.check + end + + val make : Props.t -> Jsx.element + val component : Props.t Jsx.component +end + +(** The commands: a [
] that scrolls. A press on an item, or + Enter while it is focused, calls the item's [on_action] and then this + [on_action] with its key. An item whose key [disabled_keys] holds is + disabled. While it shows no item it draws [render_empty_state], centred. *) +module List : sig + module Props : sig + type t + + val check : + ?on_action:(string -> unit) -> + ?disabled_keys:string array Solidtype.Check.value -> + ?render_empty_state:(unit -> Jsx.element) -> + ?class_:string Solidtype.Check.value -> + children:(_, [ `elements ]) Solidtype.Check.children -> + ?as_:(Solidtype.Html.Div.props -> Jsx.element) -> + ?props:t S.rest -> + (unit -> unit) Solidtype.Html.Div.check + end + + val make : Props.t -> Jsx.element + val component : Props.t Jsx.component +end + +(** Commands that belong together under a [heading]: a [
], shown while + one of its items is. *) +module Group : sig + module Props : sig + type t + + val check : + ?heading:Jsx.element Solidtype.Check.value -> + ?class_:string Solidtype.Check.value -> + children:(_, [ `elements ]) Solidtype.Check.children -> + ?as_:(Solidtype.Html.Section.props -> Jsx.element) -> + ?props:t S.rest -> + (unit -> unit) Solidtype.Html.Section.check + end + + val make : Props.t -> Jsx.element + val component : Props.t Jsx.component +end + +(** A command: a [
] of an icon, a label and a shortcut's + hint, its key [id], else one it makes. It is shown while the palette's + filter keeps its [text_value], else the text it draws; a press, or Enter + while it is focused, calls [on_action]. It is disabled while [is_disabled]. +*) +module Item : sig + module Props : sig + type t + + val check : + ?id:string Solidtype.Check.value -> + ?text_value:string Solidtype.Check.value -> + ?is_disabled:bool Solidtype.Check.value -> + ?on_action:(unit -> unit) -> + ?class_:string Solidtype.Check.value -> + ?ref:(Dom.htmlDivElement -> unit) -> + children:(_, [ `elements ]) Solidtype.Check.children -> + ?as_:(Solidtype.Html.Div.props -> Jsx.element) -> + ?props:t S.rest -> + (unit -> unit) Solidtype.Html.Div.check + end + + val make : Props.t -> Jsx.element + val component : Props.t Jsx.component +end + +(** A line between groups: a [
], hidden while it parts + nothing shown. *) +module Separator : sig + module Props : sig + type t + + val check : + ?class_:string Solidtype.Check.value -> + ?as_:(Solidtype.Html.Div.props -> Jsx.element) -> + ?props:t S.rest -> + (unit -> unit) Solidtype.Html.Div.check + end + + val make : Props.t -> Jsx.element + val component : Props.t Jsx.component +end + +(** What sits below the list, the keys' hints: a [
]. *) +module Footer : sig + module Props : sig + type t + + val check : + ?class_:string Solidtype.Check.value -> + children:(_, [ `elements ]) Solidtype.Check.children -> + ?as_:(Solidtype.Html.Div.props -> Jsx.element) -> + ?props:t S.rest -> + (unit -> unit) Solidtype.Html.Div.check + end + + val make : Props.t -> Jsx.element + val component : Props.t Jsx.component +end + +(** The palette's root, open while [is_open] while it is given, else as its + trigger and its parts open and close it, [default_open] at first; a change + calls [on_open_change]. *) +module Props : sig + type t + + val check : + ?is_open:bool Solidtype.Check.value -> + ?default_open:bool Solidtype.Check.value -> + ?on_open_change:(bool -> unit) -> + children:(_, [ `elements ]) Solidtype.Check.children -> + ?props:t S.rest -> + unit -> + unit +end + +val make : Props.t -> Jsx.element +val component : Props.t Jsx.component diff --git a/heroui/command.mlx b/heroui/command.mlx new file mode 100644 index 0000000..dde4ca9 --- /dev/null +++ b/heroui/command.mlx @@ -0,0 +1,344 @@ +module S = Solidtype.Solid +module Jsx = Solidtype.Jsx +module Headless = Solidtype_headless.Command + +type variant = [ `opaque | `blur | `transparent ] +type size = [ `sm | `md | `lg ] + +module Size_context = Solidtype.Context.Make_optional (struct + type t = size S.accessor +end) + +(* What an input group's input reads of it and of the dialog: whether it takes + the focus as the palette opens, and how it says so. *) +type focus_on_open = { wanted : bool S.accessor; take : Dom.element -> unit } + +module Focus_context = Solidtype.Context.Make_optional (struct + type t = Dom.element -> unit +end) + +module Input_group_context = Solidtype.Context.Make_optional (struct + type t = focus_on_open +end) + +external as_element : Dom.htmlInputElement -> Dom.element = "%identity" +(* An input is an element. *) + +let within part = Dialog_frame.within part ~backdrop:"Command.Backdrop" + +module Context = struct + let use = Headless.Context.use +end + +(* A class added after the palette's own, as React's className is: its look is + command.css's, in the components layer, which a utility given wins over. *) +let after base = function Some c -> base ^ " " ^ c | None -> base + +let variant_class = function + | `opaque -> "command__backdrop command__backdrop--opaque" + | `blur -> "command__backdrop command__backdrop--blur" + | `transparent -> "command__backdrop command__backdrop--transparent" + +let size_class = function + | `sm -> "command__dialog command__dialog--sm" + | `md -> "command__dialog command__dialog--md" + | `lg -> "command__dialog command__dialog--lg" + +module Backdrop = struct + let%component make ?(variant : variant = `opaque) + ?(is_dismissable : bool = true) + ?(is_keyboard_dismiss_disabled : bool = false) ?(is_open : bool option) + ?(default_open : bool option) ?(on_open_change : (bool -> unit) option) + ?(portal_container : _ Dom.element_like option) ?(class_ : string option) + ?(style : string option) + ?ref:(given_ref : (Dom.htmlDivElement -> unit) option) + ~(children : Dialog_frame.overlay_state Jsx.children) + ?(as_ : + (Solidtype.Html.Div.props -> + Dialog_frame.overlay_state -> + Solidtype.Jsx.element) + option) () = + Dialog_frame.backdrop ~slot:"command-backdrop" + ~portal_container:(fun () -> S.get portal_container) + ~class_:(fun () -> after (variant_class (S.get variant)) (S.get class_)) + ~style:(fun () -> S.get style) + ~ref:given_ref + ~dismissable:(fun () -> S.get is_dismissable) + ~keyboard_dismissable:(fun () -> not (S.get is_keyboard_dismiss_disabled)) + ~is_open:(fun () -> S.get is_open) + ~default_open:(fun () -> S.get default_open) + ~on_open_change:(fun o -> ignore (on_open_change o : unit option)) + ~as_:(fun () -> S.get as_) + ~others:(Props.others props) + (fun s -> Jsx.draw (S.get children) s) + [@@element div] +end + +module Container = struct + let%component make ?(size : size = `md) ?(class_ : string option) + ?ref:(given_ref : (Dom.htmlDivElement -> unit) option) + ~(children : Dialog_frame.overlay_state Jsx.children) + ?(as_ : + (Solidtype.Html.Div.props -> + Dialog_frame.overlay_state -> + Solidtype.Jsx.element) + option) () = + let f = within "Command.Container" in + Dialog_frame.container f ~slot:"command-container" + ~class_:(fun () -> after "command__container" (S.get class_)) + ~ref:given_ref + ~as_:(fun () -> S.get as_) + ~others:(Props.others props) + (fun s -> + + (Jsx.draw (S.get children) s) + ) + [@@element div] +end + +module Dialog = struct + let%component make ?(aria_label : string = "Command palette") + ?(input_value : string option) ?(default_input_value : string option) + ?(on_input_change : (string -> unit) option) + ?(filter : (string -> string -> bool) option) ?(class_ : string option) + ?ref:(given_ref : (Dom.htmlElement -> unit) option) + ?(on_focus_out : (Dom.focusEvent -> unit) option) + ~(children : Dialog_frame.dialog_state Jsx.children) + ?(as_ : + (Solidtype.Html.Section.props -> + Dialog_frame.dialog_state -> + Solidtype.Jsx.element) + option) () = + let f = within "Command.Dialog" in + let size = + match Size_context.use () with + | Some s -> s + | None -> S.accessor (fun () -> `md) + in + let search = ref None in + let filter = + if Option.is_some (Props.get_filter props) then + Some (fun text typed -> Option.value (filter text typed) ~default:true) + else None + in + Dialog_frame.dialog f + ~focus_on_open:(fun () -> !search) + ~aria_label:(fun () -> S.get aria_label) + ~slot:"command-dialog" ~role:`dialog + ~class_:(fun () -> after (size_class (S.get size)) (S.get class_)) + ~ref:given_ref ~on_focus_out + ~as_:(fun () -> S.get as_) + ~others:(Props.others props) + (fun s -> + ignore (on_input_change v : unit option)) + ?filter> + search := Some e)> + (Jsx.draw (S.get children) s) + + ) + [@@element section] +end + +module Header = struct + let%component make ?(class_ : string option) ~(children : Jsx.element) () = +
+ (S.get children) +
+ [@@element div] +end + +module Input_group = struct + module Prefix = struct + let%component make ?(class_ : string option) ~(children : Jsx.element) () = +
+ (S.get children) +
+ [@@element div] + end + + module Suffix = struct + let%component make ?(class_ : string option) ~(children : Jsx.element) () = +
+ (S.get children) +
+ [@@element div] + end + + module Input = struct + let%component make ?(placeholder : string = "Search commands...") + ?(aria_label : string = "Search commands") ?(class_ : string option) + ?ref:(given_ref : (Dom.htmlInputElement -> unit) option) () = + let group = Input_group_context.use () in + + ignore (given_ref e : unit option); + match group with + | Some g when S.untrack (fun () -> S.get g.wanted) -> + g.take (as_element e) + | Some _ | None -> ()) + data_slot="command-input-group-input" + props=(Props.others props) + /> + [@@element input] + end + + module Clear_button = struct + let%component make ?(class_ : string option) + ?(on_click : (Dom.mouseEvent -> unit) option) () = + let h = Headless.Context.use () in + + ignore (on_click e : unit option); + h.set_input_value ""; + h.focus_input ()) + data_slot="command-input-group-clear-button" + props=(Props.others props) + /> + [@@element button] + end + + let%component make ?(auto_focus : bool = true) ?(class_ : string option) + ~(children : Jsx.element) () = + let h = Headless.Context.use () in + let take = + match Focus_context.use () with Some take -> take | None -> fun _ -> () + in + +
+ (S.get children) +
+
+ [@@element div] +end + +module List = struct + let%component make ?(on_action : (string -> unit) option) + ?(disabled_keys : string array option) + ?(render_empty_state : (unit -> Jsx.element) option) + ?(class_ : string option) ~(children : Jsx.element) () = + ignore (on_action k : unit option)) + ?disabled_keys=(S.get disabled_keys) + ?render_empty_state=(if + Option.is_some (Props.get_render_empty_state props) + then + Some + (fun () -> +
+ (Option.value (render_empty_state ()) + ~default:Jsx.nothing) +
) + else None) + class_=(after "command__list" (S.get class_)) + data_slot="command-list" + props=(Props.others props)> + (S.get children) +
+ [@@element div] +end + +module Group = struct + let%component make ?(heading : Jsx.element option) ?(class_ : string option) + ~(children : Jsx.element) () = + + (match S.get heading with + | Some h -> + + h + + | None -> Jsx.nothing) + (S.get children) + + [@@element section] +end + +module Item = struct + let%component make ?(id : string option) ?(text_value : string option) + ?(is_disabled : bool = false) ?(on_action : (unit -> unit) option) + ?(class_ : string option) + ?ref:(given_ref : (Dom.htmlDivElement -> unit) option) + ~(children : Jsx.element) () = + let h = Headless.Context.use () in + let made = S.create_unique_id () in + let key () = Option.value (S.get id) ~default:made in + let disabled () = S.get is_disabled || h.is_disabled (key ()) in + let i = Interaction.create ~disabled () in + ignore (on_action () : unit option)) + class_=(after "command__item" (S.get class_)) + ref=(fun e -> + ignore (given_ref e : unit option); + Interaction.watch i e) + data_slot="command-item" + data_focused=(Jsx.flag + (match S.get h.focused_key with + | Some k -> String.equal k (key ()) + | None -> false)) + data_hovered=(Jsx.flag (Interaction.hovered i)) + data_pressed=(Jsx.flag (Interaction.pressed i)) + data_disabled=(Jsx.flag (disabled ())) + props=(Props.others props)> + (S.get children) + + [@@element div] +end + +module Separator = struct + let%component make ?(class_ : string option) () = + + [@@element div] +end + +module Footer = struct + let%component make ?(class_ : string option) ~(children : Jsx.element) () = +
+ (S.get children) +
+ [@@element div] +end + +let%component make ?(is_open : bool option) ?(default_open : bool option) + ?(on_open_change : (bool -> unit) option) ~(children : Jsx.element) () = + Dialog_frame.root + ~is_open:(fun () -> S.get is_open) + ~default_open:(fun () -> S.get default_open) + ~on_open_change:(fun o -> ignore (on_open_change o : unit option)) + (fun () -> S.get children) diff --git a/heroui/date_field.mli b/heroui/date_field.mli index 70dd8ca..80ee4a3 100644 --- a/heroui/date_field.mli +++ b/heroui/date_field.mli @@ -24,12 +24,12 @@ by [CalendarDate.make 2026 11 21], [parse_date "2026-11-21"] or [today zone]. A date with a time is {!Date_time}'s, and one in a zone {!Zoned}'s, each given back as the kind it was given: the three share the - parts below. A [value] of [Js.Nullable.null] controls a field empty, as - React's [null] does, and [on_change] is told [None] once every segment is - erased. It is invalid while [is_invalid], and while its value is before - [min_value], after [max_value] or a day [is_date_unavailable] refuses, which - a {!Form} refuses to submit. A part is used inside a field: elsewhere it - raises [Invalid_argument] as it is made. *) + parts below. A [value] of [Js.Null.empty] controls a field empty, as React's + [null] does, and [on_change] is told [None] once every segment is erased. It + is invalid while [is_invalid], and while its value is before [min_value], + after [max_value] or a day [is_date_unavailable] refuses, which a {!Form} + refuses to submit. A part is used inside a field: elsewhere it raises + [Invalid_argument] as it is made. *) module S := Solidtype.Solid module Jsx := Solidtype.Jsx @@ -179,7 +179,7 @@ module Props : sig type t val check : - ?value:V.t Js.Nullable.t Solidtype.Check.value -> + ?value:V.t Js.Null.t Solidtype.Check.value -> ?default_value:V.t Solidtype.Check.value -> ?on_change:(V.t option -> unit) -> ?placeholder_value:V.t Solidtype.Check.value -> @@ -221,7 +221,7 @@ module Date_time : sig type t val check : - ?value:V.t Js.Nullable.t Solidtype.Check.value -> + ?value:V.t Js.Null.t Solidtype.Check.value -> ?default_value:V.t Solidtype.Check.value -> ?on_change:(V.t option -> unit) -> ?placeholder_value:V.t Solidtype.Check.value -> @@ -266,7 +266,7 @@ module Zoned : sig type t val check : - ?value:V.t Js.Nullable.t Solidtype.Check.value -> + ?value:V.t Js.Null.t Solidtype.Check.value -> ?default_value:V.t Solidtype.Check.value -> ?on_change:(V.t option -> unit) -> placeholder_value:V.t Solidtype.Check.value -> @@ -320,7 +320,7 @@ type 'v root = { val root : kind:'v kind -> - value:'v Js.Nullable.t option S.accessor -> + value:'v Js.Null.t option S.accessor -> default_value:'v option S.accessor -> on_change:('v option -> unit option) -> min_value:'v option S.accessor -> diff --git a/heroui/date_field.mlx b/heroui/date_field.mlx index ba55cac..12f982f 100644 --- a/heroui/date_field.mlx +++ b/heroui/date_field.mlx @@ -285,14 +285,14 @@ type 'v root = { leave : Dom.focusEvent -> unit; } -let root (type v) ~(kind : v kind) ~(value : v Js.Nullable.t option S.accessor) +let root (type v) ~(kind : v kind) ~(value : v Js.Null.t option S.accessor) ~(default_value : v option S.accessor) ~(on_change : v option -> unit option) ~(min_value : v option S.accessor) ~(max_value : v option S.accessor) ~(is_date_unavailable : v -> bool option) ~is_invalid ~name ~(on_blur : Dom.focusEvent -> unit option) : v root = let current, set = Field.controlled - ~value:(fun () -> Option.map Js.Nullable.toOption (S.get value)) + ~value:(fun () -> Option.map Js.Null.toOption (S.get value)) ~default:(S.get default_value) ~on_change:(fun v -> ignore (on_change v : unit option)) in @@ -327,11 +327,7 @@ let root (type v) ~(kind : v kind) ~(value : v Js.Nullable.t option S.accessor) ignore (on_blur e : unit option)); } -let nullable = function - | Some v -> Js.Nullable.return v - | None -> Js.Nullable.null - -let%component make ?(value : D.Types.CalendarDate.t Js.Nullable.t option) +let%component make ?(value : D.Types.CalendarDate.t Js.Null.t option) ?(default_value : D.Types.CalendarDate.t option) ?(on_change : (D.Types.CalendarDate.t option -> unit) option) ?(placeholder_value : D.Types.CalendarDate.t option) @@ -362,7 +358,7 @@ let%component make ?(value : D.Types.CalendarDate.t Js.Nullable.t option) in unit) option) ?(placeholder_value : V.t option) ?(min_value : V.t option) ?(max_value : V.t option) ?(is_date_unavailable : (V.t -> bool) option) @@ -431,7 +427,7 @@ module Date_time = struct in unit) option) ~(placeholder_value : V.t) ?(min_value : V.t option) ?(max_value : V.t option) ?(is_date_unavailable : (V.t -> bool) option) @@ -504,7 +500,7 @@ module Zoned = struct in + ?value:D.Types.CalendarDate.t Js.Null.t Solidtype.Check.value -> ?default_value:D.Types.CalendarDate.t Solidtype.Check.value -> ?on_change:(D.Types.CalendarDate.t option -> unit) -> ?placeholder_value:D.Types.CalendarDate.t Solidtype.Check.value -> @@ -103,7 +103,7 @@ module Date_time : sig type t val check : - ?value:D.Types.CalendarDateTime.t Js.Nullable.t Solidtype.Check.value -> + ?value:D.Types.CalendarDateTime.t Js.Null.t Solidtype.Check.value -> ?default_value:D.Types.CalendarDateTime.t Solidtype.Check.value -> ?on_change:(D.Types.CalendarDateTime.t option -> unit) -> ?placeholder_value:D.Types.CalendarDateTime.t Solidtype.Check.value -> @@ -155,7 +155,7 @@ module Zoned : sig type t val check : - ?value:D.Types.ZonedDateTime.t Js.Nullable.t Solidtype.Check.value -> + ?value:D.Types.ZonedDateTime.t Js.Null.t Solidtype.Check.value -> ?default_value:D.Types.ZonedDateTime.t Solidtype.Check.value -> ?on_change:(D.Types.ZonedDateTime.t option -> unit) -> placeholder_value:D.Types.ZonedDateTime.t Solidtype.Check.value -> diff --git a/heroui/date_picker.mlx b/heroui/date_picker.mlx index d0c09c3..a1baf05 100644 --- a/heroui/date_picker.mlx +++ b/heroui/date_picker.mlx @@ -59,7 +59,7 @@ type 'v state = { module Date_root = struct module V = D.Types.CalendarDate - let%component make ?(value : V.t Js.Nullable.t option) + let%component make ?(value : V.t Js.Null.t option) ?(default_value : V.t option) ?(on_change : (V.t option -> unit) option) ?(placeholder_value : V.t option) ?(min_value : V.t option) ?(max_value : V.t option) ?(is_date_unavailable : (V.t -> bool) option) @@ -96,7 +96,7 @@ module Date_root = struct } in unit) option) ?(placeholder_value : V.t option) ?(granularity : [ `day | `hour | `minute | `second ] option) @@ -181,7 +181,7 @@ module Date_time = struct } in unit) option) ~(placeholder_value : V.t) ?(granularity : [ `day | `hour | `minute | `second ] option) @@ -269,7 +269,7 @@ module Zoned = struct } in + ?value:D.Types.CalendarDate.t range Js.Null.t Solidtype.Check.value -> ?default_value:D.Types.CalendarDate.t range Solidtype.Check.value -> ?on_change:(D.Types.CalendarDate.t range option -> unit) -> ?start_name:string Solidtype.Check.value -> @@ -116,8 +117,7 @@ module Date_time : sig type t val check : - ?value: - D.Types.CalendarDateTime.t range Js.Nullable.t Solidtype.Check.value -> + ?value:D.Types.CalendarDateTime.t range Js.Null.t Solidtype.Check.value -> ?default_value:D.Types.CalendarDateTime.t range Solidtype.Check.value -> ?on_change:(D.Types.CalendarDateTime.t range option -> unit) -> ?start_name:string Solidtype.Check.value -> @@ -167,7 +167,7 @@ module Zoned : sig type t val check : - ?value:D.Types.ZonedDateTime.t range Js.Nullable.t Solidtype.Check.value -> + ?value:D.Types.ZonedDateTime.t range Js.Null.t Solidtype.Check.value -> ?default_value:D.Types.ZonedDateTime.t range Solidtype.Check.value -> ?on_change:(D.Types.ZonedDateTime.t range option -> unit) -> ?start_name:string Solidtype.Check.value -> diff --git a/heroui/date_range_picker.mlx b/heroui/date_range_picker.mlx index 41aad26..9c52ce3 100644 --- a/heroui/date_range_picker.mlx +++ b/heroui/date_range_picker.mlx @@ -31,7 +31,7 @@ type 'v root = { } let root ~(compare : 'v -> 'v -> float) - ~(value : 'v range Js.Nullable.t option S.accessor) + ~(value : 'v range Js.Null.t option S.accessor) ~(default_value : 'v range option S.accessor) ~(on_change : 'v range option -> unit option) ~(min_value : 'v option S.accessor) ~(max_value : 'v option S.accessor) @@ -39,7 +39,7 @@ let root ~(compare : 'v -> 'v -> float) ~(on_blur : Dom.focusEvent -> unit option) = let current, set = Field.controlled - ~value:(fun () -> Option.map Js.Nullable.toOption (S.get value)) + ~value:(fun () -> Option.map Js.Null.toOption (S.get value)) ~default:(S.get default_value) ~on_change:(fun v -> ignore (on_change v : unit option)) in @@ -75,7 +75,7 @@ let root ~(compare : 'v -> 'v -> float) module Date_root = struct module V = D.Types.CalendarDate - let%component make ?(value : V.t Headless_field.range Js.Nullable.t option) + let%component make ?(value : V.t Headless_field.range Js.Null.t option) ?(default_value : V.t Headless_field.range option) ?(on_change : (V.t Headless_field.range option -> unit) option) ?(start_name : string option) ?(end_name : string option) @@ -113,7 +113,7 @@ module Date_root = struct } in unit) option) ?(start_name : string option) ?(end_name : string option) @@ -200,7 +200,7 @@ module Date_time = struct } in unit) option) ?(start_name : string option) ?(end_name : string option) @@ -290,7 +290,7 @@ module Zoned = struct } in string) -> t -> slot:string -> class_:(unit -> string) -> - placement:(unit -> string) -> ref:(Dom.htmlDivElement -> unit option) -> as_: (unit -> (Solidtype.Html.Div.props -> overlay_state -> Jsx.element) option) -> others:_ S.rest -> (overlay_state -> Jsx.element) -> Jsx.element -(** A [
] that enters and exits with the backdrop. *) +(** A [
] that enters and exits with the backdrop, its [data-placement] the + [placement] given. *) val dialog : ?on_element:(Dom.element -> unit) -> + ?focus_on_open:(unit -> Dom.element option) -> + ?placement:(unit -> string) -> + ?aria_label:(unit -> string) -> t -> slot:string -> role:Solidtype.Html.Section.role -> class_:(unit -> string) -> - placement:(unit -> string) -> ref:(Dom.htmlElement -> unit option) -> on_focus_out:(Dom.focusEvent -> unit option) -> as_: @@ -117,7 +120,9 @@ val dialog : (dialog_state -> Jsx.element) -> Jsx.element (** [on_element] is given the dialog's element as it is made, for a part that - does more with it, as a drawer is dragged. *) + does more with it, as a drawer is dragged. As it opens, the focus goes to + what [focus_on_open] gives, else to the dialog. It is named [aria_label] + where that is given. *) val heading : slot:string -> diff --git a/heroui/dialog_frame.mlx b/heroui/dialog_frame.mlx index bc76a87..a5be28e 100644 --- a/heroui/dialog_frame.mlx +++ b/heroui/dialog_frame.mlx @@ -254,7 +254,11 @@ let backdrop ~slot ~portal_container ~class_ ~style ~ref:given_ref ~dismissable (shown ()) -let container (f : t) ~slot ~class_ ~placement ~ref:given_ref ~as_ ~others +let placed = function + | Some placement -> Jsx.data (placement ()) + | None -> Jsx.flag false + +let container ?placement (f : t) ~slot ~class_ ~ref:given_ref ~as_ ~others children = let o = f.overlay in
ignore (given_ref e : unit option); Overlay.watch o e) - data_placement=(Jsx.data (placement ())) + data_placement=(placed placement) data_slot=(Jsx.data slot) data_entering=(Jsx.flag (Overlay.entering o)) data_exiting=(Jsx.flag (Overlay.exiting o)) @@ -271,8 +275,9 @@ let container (f : t) ~slot ~class_ ~placement ~ref:given_ref ~as_ ~others (children (overlay_state o))
-let dialog ?(on_element = fun (_ : Dom.element) -> ()) (f : t) ~slot ~role - ~class_ ~placement ~ref:given_ref ~on_focus_out ~as_ ~others children = +let dialog ?(on_element = fun (_ : Dom.element) -> ()) + ?(focus_on_open = fun () -> None) ?placement ?aria_label (f : t) ~slot ~role + ~class_ ~ref:given_ref ~on_focus_out ~as_ ~others children = let content = ref None in Overlay.return_focus ~is_open:(fun () -> not (Overlay.exiting f.overlay)) @@ -282,11 +287,14 @@ let dialog ?(on_element = fun (_ : Dom.element) -> ()) (f : t) ~slot ~role (* React Aria's dialog takes the focus as it opens, where Kobalte gives it to the first button inside. *) let focus_dialog e = - match !content with - | Some c -> + match (focus_on_open (), !content) with + | Some inside, _ -> + prevent_default e; + Overlay.focus inside + | None, Some c -> prevent_default e; Overlay.focus c - | None -> () + | None, None -> () in ()) (f : t) ~slot ~role "section") class_=(class_ ()) role + ?aria_label=(Option.map (fun name -> name ()) aria_label) ref=(fun e -> ignore (given_ref e : unit option); let e = as_element e in @@ -317,7 +326,7 @@ let dialog ?(on_element = fun (_ : Dom.element) -> ()) (f : t) ~slot ~role Option.is_none (Js.Null.toOption (related_target e)) && not (Overlay.exiting f.overlay) then Overlay.focus (target e)) - data_placement=(Jsx.data (placement ())) + data_placement=(placed placement) data_slot=(Jsx.data slot) props=others> (if diff --git a/heroui/extras.css b/heroui/extras.css index 48fd9c1..62e53ff 100644 --- a/heroui/extras.css +++ b/heroui/extras.css @@ -4,3 +4,4 @@ @import "./number-stepper.css" layer(components); @import "./table.css" layer(components); @import "./data-grid.css" layer(components); +@import "./command.css" layer(components); diff --git a/heroui/number_field.mli b/heroui/number_field.mli index d75b8a7..635ad54 100644 --- a/heroui/number_field.mli +++ b/heroui/number_field.mli @@ -22,7 +22,9 @@ A value is kept on the grid of a [step] it is given, counted from its minimum, and else within its range alone, as React Aria keeps it; its buttons and arrow keys step it by [step], else by 1, or by a hundredth for a - percent. + percent. Text typed is its value once Enter or a focus that leaves commits + it, which [on_change] is told, as React Aria's; a step is told at once. A + [value] of [nan] controls it empty. The wheel steps it so too while its input has the focus, as {!Solidtype_headless.Number_field} turns it, unless [is_wheel_disabled]. diff --git a/heroui/number_field.mlx b/heroui/number_field.mlx index 17d81c6..031e368 100644 --- a/heroui/number_field.mlx +++ b/heroui/number_field.mlx @@ -29,12 +29,16 @@ type field = { current : float S.accessor; can_increment : bool S.accessor; can_decrement : bool S.accessor; + typed : unit -> unit; + commit : unit -> unit; } module Context = Solidtype.Context.Make_optional (struct type t = field end) +external queue_microtask : (unit -> unit) -> unit = "queueMicrotask" + (* The decimals [x] is written with, a 1e-7 as a 0.0000001: the precision a value on a grid of its step, counted from its minimum, is rounded to. *) let decimals x = @@ -176,8 +180,11 @@ module Input = struct let stepping e = ignore (on_key_down e : unit option); match key e with + | "Enter" -> + f.commit (); + S.set set_editing false | "ArrowUp" | "ArrowDown" | "PageUp" | "PageDown" | "Home" | "End" - | "Enter" | "Escape" -> + | "Escape" -> S.set set_editing false | _ -> () in @@ -197,10 +204,12 @@ module Input = struct data_editing=(Jsx.flag (f.look_given && S.get editing)) on_input=(fun e -> ignore (on_input e : unit option); + f.typed (); S.set set_editing true) on_key_down=stepping on_focus_out=(fun e -> ignore (on_focus_out e : unit option); + f.commit (); S.set set_editing false) data_hovered=(Jsx.flag (Interaction.hovered i)) data_focused=(Jsx.flag (Interaction.focused i)) @@ -365,13 +374,34 @@ let%component make ?(variant : [ `primary | `secondary ] option) S.accessor (fun () -> S.get is_invalid || Option.is_some (S.get in_form.error)) in - let change v = + let committed v = let v = snapped v in let now = S.get current in if not (Float.equal v now || (Float.is_nan v && Float.is_nan now)) then ( in_form.changed (); set v) in + (* Text typed is the value once it is committed, by Enter or as the focus + leaves, as React Aria's field commits it; a step, by the keys, a button + or the wheel, at once. The input marks the change its typing makes, which + Kobalte tells within the same event, and a key it refuses makes none. *) + let typing = ref false in + let pending = ref None in + let change v = + if !typing then ( + typing := false; + pending := Some v) + else ( + pending := None; + committed v) + in + let commit () = + match !pending with + | Some v -> + pending := None; + committed v + | None -> () + in (* A button steps the value while its step would move it. *) let can moved = S.accessor (fun () -> @@ -428,6 +458,11 @@ let%component make ?(variant : [ `primary | `secondary ] option) invalid; can_increment = can (fun v -> v +. by ()); can_decrement = can (fun v -> v -. by ()); + typed = + (fun () -> + typing := true; + queue_microtask (fun () -> typing := false)); + commit; } in let state () = diff --git a/heroui/radio_group.mli b/heroui/radio_group.mli index 3332042..9a8466c 100644 --- a/heroui/radio_group.mli +++ b/heroui/radio_group.mli @@ -4,8 +4,9 @@ as React Aria writes it. A {!Label} inside it, but outside a radio's content, is the group's, which names it, and a {!Description} the group's, which describes it. It is disabled as [is_disabled] says, else as the - {!Fieldset} it is in is. Its props are named as HeroUI's documentation names - them. + {!Fieldset} it is in is. A [value] of [Js.Null.empty] controls it with none + chosen, as React's [null] does. Its props are named as HeroUI's + documentation names them. Its page, with its examples: [$(opam var solidtype:doc)/pages/heroui/radio-group.md]. @@ -66,7 +67,7 @@ module Props : sig val check : ?variant:[ `primary | `secondary ] Solidtype.Check.value -> - ?value:string Solidtype.Check.value -> + ?value:string Js.Null.t Solidtype.Check.value -> ?default_value:string Solidtype.Check.value -> ?on_change:(string -> unit) -> ?orientation:[ `horizontal | `vertical ] Solidtype.Check.value -> diff --git a/heroui/radio_group.mlx b/heroui/radio_group.mlx index 9354e8d..e68dcdf 100644 --- a/heroui/radio_group.mlx +++ b/heroui/radio_group.mlx @@ -108,7 +108,7 @@ type state = { } let%component make ?(variant : [ `primary | `secondary ] = `primary) - ?(value : string option) ?(default_value : string option) + ?(value : string Js.Null.t option) ?(default_value : string option) ?(on_change : (string -> unit) option) ?(orientation : [ `horizontal | `vertical ] = `vertical) ?(is_disabled : bool option) ?(is_invalid : bool = false) @@ -140,7 +140,7 @@ let%component make ?(variant : [ `primary | `secondary ] = `primary) none is Kobalte's own empty value, as a form's reset leaves it. *) let chosen, choose = Field.controlled - ~value:(fun () -> Option.map Option.some (S.get value)) + ~value:(fun () -> Option.map Js.Null.toOption (S.get value)) ~default:(S.get default_value) ~on_change: (Option.iter (fun v -> diff --git a/heroui/select.mlx b/heroui/select.mlx index 5ad27c3..bc208fb 100644 --- a/heroui/select.mlx +++ b/heroui/select.mlx @@ -480,11 +480,9 @@ let%component make ?(placeholder : string = "Select an item") option_value=(fun o -> Types.Either.left (List_box.key o)) option_text_value=List_box.text option_disabled=(fun o -> disabled_key (List_box.key o)) - value=(* [null], an empty selection Kobalte keeps: [undefined] - would be no value given. *) - (match S.get selected with - | o :: _ -> Js.Nullable.return o - | [] -> Js.Nullable.null) + value=(match S.get selected with + | o :: _ -> Js.Null.return o + | [] -> Js.Null.empty) on_change=(fun o -> change (match Js.Nullable.toOption o with diff --git a/heroui/time_field.mli b/heroui/time_field.mli index e743974..1797202 100644 --- a/heroui/time_field.mli +++ b/heroui/time_field.mli @@ -26,7 +26,7 @@ [Time.make ~hour:14 ~minute:30 ()] or [parse_time "14:30"]. A time on a date is {!Date_time}'s, and a time in a zone {!Zoned}'s, each given back as the kind it was given: the three share the parts below. A [value] of - [Js.Nullable.null] controls a field empty, as React's [null] does, and + [Js.Null.empty] controls a field empty, as React's [null] does, and [on_change] is told [None] once every segment is erased. Its locale is the one an [I18nProvider] names, else [en-GB], whatever the browser reads, as a {!Date_field}'s is. A part is used inside a field: elsewhere it raises @@ -163,7 +163,7 @@ module Props : sig val check : ?value: - Solidtype_internationalized_date.Types.Time.t Js.Nullable.t + Solidtype_internationalized_date.Types.Time.t Js.Null.t Solidtype.Check.value -> ?default_value: Solidtype_internationalized_date.Types.Time.t Solidtype.Check.value -> @@ -210,7 +210,7 @@ module Date_time : sig val check : ?value: - Solidtype_internationalized_date.Types.CalendarDateTime.t Js.Nullable.t + Solidtype_internationalized_date.Types.CalendarDateTime.t Js.Null.t Solidtype.Check.value -> ?default_value: Solidtype_internationalized_date.Types.CalendarDateTime.t @@ -265,7 +265,7 @@ module Zoned : sig val check : ?value: - Solidtype_internationalized_date.Types.ZonedDateTime.t Js.Nullable.t + Solidtype_internationalized_date.Types.ZonedDateTime.t Js.Null.t Solidtype.Check.value -> ?default_value: Solidtype_internationalized_date.Types.ZonedDateTime.t diff --git a/heroui/time_field.mlx b/heroui/time_field.mlx index 5502906..d9040ea 100644 --- a/heroui/time_field.mlx +++ b/heroui/time_field.mlx @@ -354,7 +354,7 @@ type state = { } let field (type v) ~(kind : v kind) ~(fallback : v S.accessor) - ~(value : v Js.Nullable.t option S.accessor) + ~(value : v Js.Null.t option S.accessor) ~(default_value : v option S.accessor) ~(on_change : v option -> unit option) ~(placeholder_value : v option S.accessor) @@ -369,7 +369,7 @@ let field (type v) ~(kind : v kind) ~(fallback : v S.accessor) in let current, set = Field.controlled - ~value:(fun () -> Option.map Js.Nullable.toOption (S.get value)) + ~value:(fun () -> Option.map Js.Null.toOption (S.get value)) ~default:(S.get default_value) ~on_change:(fun v -> in_form.changed (); @@ -646,7 +646,7 @@ module In_locale = struct else (S.get children) end -let%component make ?(value : Time.t Js.Nullable.t option) +let%component make ?(value : Time.t Js.Null.t option) ?(default_value : Time.t option) ?(on_change : (Time.t option -> unit) option) ?(placeholder_value : Time.t option) ?(min_value : Time.t option) @@ -682,7 +682,7 @@ let%component make ?(value : Time.t Js.Nullable.t option) module Date_time = struct module V = D.Types.CalendarDateTime - let%component make ?(value : V.t Js.Nullable.t option) + let%component make ?(value : V.t Js.Null.t option) ?(default_value : V.t option) ?(on_change : (V.t option -> unit) option) ~(placeholder_value : V.t) ?(min_value : Time.t option) ?(max_value : Time.t option) @@ -719,7 +719,7 @@ end module Zoned = struct module V = D.Types.ZonedDateTime - let%component make ?(value : V.t Js.Nullable.t option) + let%component make ?(value : V.t Js.Null.t option) ?(default_value : V.t option) ?(on_change : (V.t option -> unit) option) ~(placeholder_value : V.t) ?(min_value : Time.t option) ?(max_value : Time.t option) ?(hide_time_zone : bool = false) diff --git a/heroui/toggle_button_group.mlx b/heroui/toggle_button_group.mlx index 376192b..07aea4e 100644 --- a/heroui/toggle_button_group.mlx +++ b/heroui/toggle_button_group.mlx @@ -112,8 +112,8 @@ let%component make ?(selection_mode : [ `single | `multiple ] = `single) | `single -> false) fallback=( Js.Nullable.null - | ks -> Js.Nullable.return ks.(0)) + | [||] -> Js.Null.empty + | ks -> Js.Null.return ks.(0)) orientation=(S.get orientation) disabled=(all_disabled ()) ?aria_label=(S.get aria_label) diff --git a/kobalte/solidtype_kobalte.ml b/kobalte/solidtype_kobalte.ml index 96c9c1a..a14ca48 100644 --- a/kobalte/solidtype_kobalte.ml +++ b/kobalte/solidtype_kobalte.ml @@ -1429,7 +1429,7 @@ module Combobox = struct module Root = struct module Props = struct external check : - ?value:'option Js.Nullable.t Solidtype.Check.value -> + ?value:'option Js.Null.t Solidtype.Check.value -> ?default_value:'option Solidtype.Check.value -> ?on_change:('option Js.Nullable.t -> unit) -> ?id:string Solidtype.Check.value -> @@ -2590,7 +2590,7 @@ module FileField = struct ?on_file_accept:(Types.File.t array -> unit) -> ?on_file_reject:(Types.FileRejection.t array -> unit) -> ?on_file_change:(Types.Details.t -> unit) -> - ?validate:(Types.File.t -> [ `TOO_MANY_FILES | `FILE_INVALID_TYPE | `FILE_TOO_LARGE | `FILE_TOO_SMALL ] array Js.Nullable.t) -> + ?validate:(Types.File.t -> [ `TOO_MANY_FILES | `FILE_INVALID_TYPE | `FILE_TOO_LARGE | `FILE_TOO_SMALL ] array Js.Null.t) -> ?id:string Solidtype.Check.value -> ?name:string Solidtype.Check.value -> ?validation_state:[ `valid | `invalid ] Solidtype.Check.value -> @@ -3232,7 +3232,7 @@ module Menubar = struct module Props = struct external check : ?default_value:string Solidtype.Check.value -> - ?value:string Js.Nullable.t Solidtype.Check.value -> + ?value:string Js.Null.t Solidtype.Check.value -> ?on_value_change:(string Js.Nullable.t -> unit) -> ?loop:bool Solidtype.Check.value -> ?focus_on_alt:bool Solidtype.Check.value -> @@ -3688,7 +3688,7 @@ module NavigationMenu = struct ?auto_focus_menu:bool Solidtype.Check.value -> ?on_auto_focus_menu_change:bool Solidtype.Solid.setter Solidtype.Check.value -> ?default_value:string Solidtype.Check.value -> - ?value:string Js.Nullable.t Solidtype.Check.value -> + ?value:string Js.Null.t Solidtype.Check.value -> ?on_value_change:(string Js.Nullable.t -> unit) -> ?loop:bool Solidtype.Check.value -> ?focus_on_alt:bool Solidtype.Check.value -> @@ -4834,7 +4834,7 @@ module Search = struct module Root = struct module Props = struct external check : - ?value:'option Js.Nullable.t Solidtype.Check.value -> + ?value:'option Js.Null.t Solidtype.Check.value -> ?default_value:'option Solidtype.Check.value -> ?on_change:('option Js.Nullable.t -> unit) -> ?id:string Solidtype.Check.value -> @@ -5354,7 +5354,7 @@ module Select = struct module Root = struct module Props = struct external check : - ?value:'option Js.Nullable.t Solidtype.Check.value -> + ?value:'option Js.Null.t Solidtype.Check.value -> ?default_value:'option Solidtype.Check.value -> ?on_change:('option Js.Nullable.t -> unit) -> ?id:string Solidtype.Check.value -> @@ -6325,7 +6325,7 @@ module ToggleGroup = struct module Root = struct module Props = struct external check : - ?value:string Js.Nullable.t Solidtype.Check.value -> + ?value:string Js.Null.t Solidtype.Check.value -> ?default_value:string Solidtype.Check.value -> ?on_change:(string Js.Nullable.t -> unit) -> ?disabled:bool Solidtype.Check.value -> diff --git a/kobalte/solidtype_kobalte.mli b/kobalte/solidtype_kobalte.mli index 670df4d..907c962 100644 --- a/kobalte/solidtype_kobalte.mli +++ b/kobalte/solidtype_kobalte.mli @@ -1424,7 +1424,7 @@ module Combobox : sig module Root : sig module Props : sig external check : - ?value:'option Js.Nullable.t Solidtype.Check.value -> + ?value:'option Js.Null.t Solidtype.Check.value -> ?default_value:'option Solidtype.Check.value -> ?on_change:('option Js.Nullable.t -> unit) -> ?id:string Solidtype.Check.value -> @@ -2585,7 +2585,7 @@ module FileField : sig ?on_file_accept:(Types.File.t array -> unit) -> ?on_file_reject:(Types.FileRejection.t array -> unit) -> ?on_file_change:(Types.Details.t -> unit) -> - ?validate:(Types.File.t -> [ `TOO_MANY_FILES | `FILE_INVALID_TYPE | `FILE_TOO_LARGE | `FILE_TOO_SMALL ] array Js.Nullable.t) -> + ?validate:(Types.File.t -> [ `TOO_MANY_FILES | `FILE_INVALID_TYPE | `FILE_TOO_LARGE | `FILE_TOO_SMALL ] array Js.Null.t) -> ?id:string Solidtype.Check.value -> ?name:string Solidtype.Check.value -> ?validation_state:[ `valid | `invalid ] Solidtype.Check.value -> @@ -3227,7 +3227,7 @@ module Menubar : sig module Props : sig external check : ?default_value:string Solidtype.Check.value -> - ?value:string Js.Nullable.t Solidtype.Check.value -> + ?value:string Js.Null.t Solidtype.Check.value -> ?on_value_change:(string Js.Nullable.t -> unit) -> ?loop:bool Solidtype.Check.value -> ?focus_on_alt:bool Solidtype.Check.value -> @@ -3683,7 +3683,7 @@ module NavigationMenu : sig ?auto_focus_menu:bool Solidtype.Check.value -> ?on_auto_focus_menu_change:bool Solidtype.Solid.setter Solidtype.Check.value -> ?default_value:string Solidtype.Check.value -> - ?value:string Js.Nullable.t Solidtype.Check.value -> + ?value:string Js.Null.t Solidtype.Check.value -> ?on_value_change:(string Js.Nullable.t -> unit) -> ?loop:bool Solidtype.Check.value -> ?focus_on_alt:bool Solidtype.Check.value -> @@ -4829,7 +4829,7 @@ module Search : sig module Root : sig module Props : sig external check : - ?value:'option Js.Nullable.t Solidtype.Check.value -> + ?value:'option Js.Null.t Solidtype.Check.value -> ?default_value:'option Solidtype.Check.value -> ?on_change:('option Js.Nullable.t -> unit) -> ?id:string Solidtype.Check.value -> @@ -5349,7 +5349,7 @@ module Select : sig module Root : sig module Props : sig external check : - ?value:'option Js.Nullable.t Solidtype.Check.value -> + ?value:'option Js.Null.t Solidtype.Check.value -> ?default_value:'option Solidtype.Check.value -> ?on_change:('option Js.Nullable.t -> unit) -> ?id:string Solidtype.Check.value -> @@ -6320,7 +6320,7 @@ module ToggleGroup : sig module Root : sig module Props : sig external check : - ?value:string Js.Nullable.t Solidtype.Check.value -> + ?value:string Js.Null.t Solidtype.Check.value -> ?default_value:string Solidtype.Check.value -> ?on_change:(string Js.Nullable.t -> unit) -> ?disabled:bool Solidtype.Check.value -> diff --git a/kobalte/types.ml b/kobalte/types.ml index a01d7f3..025c305 100644 --- a/kobalte/types.ml +++ b/kobalte/types.ml @@ -745,7 +745,7 @@ module HoverCardContextValue = struct external cancel_opening : t -> unit -> unit = "cancelOpening" [@@mel.send] external cancel_closing : t -> unit -> unit = "cancelClosing" [@@mel.send] external close : t -> unit -> unit = "close" [@@mel.send] - external is_target_on_hover_card : t -> Dom.node Js.Nullable.t -> bool = "isTargetOnHoverCard" [@@mel.send] + external is_target_on_hover_card : t -> Dom.node Js.Null.t -> bool = "isTargetOnHoverCard" [@@mel.send] external set_trigger_ref : t -> Dom.htmlElement -> unit = "setTriggerRef" [@@mel.send] external set_content_ref : t -> Dom.htmlElement -> unit = "setContentRef" [@@mel.send] external make : dataset:(t_hover_card_data_set Solidtype.Solid.accessor [@mel.as "dataset"]) -> is_open:(bool Solidtype.Solid.accessor [@mel.as "isOpen"]) -> content_present:(bool Solidtype.Solid.accessor [@mel.as "contentPresent"]) -> open_with_delay:((unit -> unit) [@mel.as "openWithDelay"]) -> close_with_delay:((unit -> unit) [@mel.as "closeWithDelay"]) -> cancel_opening:((unit -> unit) [@mel.as "cancelOpening"]) -> cancel_closing:((unit -> unit) [@mel.as "cancelClosing"]) -> close:((unit -> unit) [@mel.as "close"]) -> is_target_on_hover_card:((Dom.node Js.Nullable.t -> bool) [@mel.as "isTargetOnHoverCard"]) -> set_trigger_ref:((Dom.htmlElement -> unit) [@mel.as "setTriggerRef"]) -> set_content_ref:((Dom.htmlElement -> unit) [@mel.as "setContentRef"]) -> unit -> t = "" [@@mel.obj] @@ -1391,7 +1391,7 @@ module TooltipContextValue = struct external generate_id : t -> string -> string = "generateId" [@@mel.send] external register_content_id_js : t -> (string -> (unit -> unit [@u]) [@u]) = "registerContentId" [@@mel.get] let register_content_id o x0 = let r = (register_content_id_js o) x0 [@u] in fun () -> r () [@u] - external is_target_on_tooltip : t -> Dom.node Js.Nullable.t -> bool = "isTargetOnTooltip" [@@mel.send] + external is_target_on_tooltip : t -> Dom.node Js.Null.t -> bool = "isTargetOnTooltip" [@@mel.send] external set_trigger_ref : t -> Dom.htmlElement -> unit = "setTriggerRef" [@@mel.send] external set_content_ref : t -> Dom.htmlElement -> unit = "setContentRef" [@@mel.send] external make_js : dataset:(t_tooltip_data_set Solidtype.Solid.accessor [@mel.as "dataset"]) -> is_open:(bool Solidtype.Solid.accessor [@mel.as "isOpen"]) -> is_disabled:(bool Solidtype.Solid.accessor [@mel.as "isDisabled"]) -> trigger_on_focus_only:(bool Solidtype.Solid.accessor [@mel.as "triggerOnFocusOnly"]) -> content_id:(string option Solidtype.Solid.accessor [@mel.as "contentId"]) -> content_present:(bool Solidtype.Solid.accessor [@mel.as "contentPresent"]) -> open_tooltip:((bool option -> unit) [@mel.as "openTooltip"]) -> hide_tooltip:((bool option -> unit) [@mel.as "hideTooltip"]) -> cancel_opening:((unit -> unit) [@mel.as "cancelOpening"]) -> generate_id:((string -> string) [@mel.as "generateId"]) -> register_content_id:((string -> (unit -> unit)) [@mel.as "registerContentId"]) -> is_target_on_tooltip:((Dom.node Js.Nullable.t -> bool) [@mel.as "isTargetOnTooltip"]) -> set_trigger_ref:((Dom.htmlElement -> unit) [@mel.as "setTriggerRef"]) -> set_content_ref:((Dom.htmlElement -> unit) [@mel.as "setContentRef"]) -> unit -> t = "" [@@mel.obj] diff --git a/kobalte/types.mli b/kobalte/types.mli index 39cdc7a..e6f9b61 100644 --- a/kobalte/types.mli +++ b/kobalte/types.mli @@ -725,7 +725,7 @@ module HoverCardContextValue : sig external cancel_opening : t -> unit -> unit = "cancelOpening" [@@mel.send] external cancel_closing : t -> unit -> unit = "cancelClosing" [@@mel.send] external close : t -> unit -> unit = "close" [@@mel.send] - external is_target_on_hover_card : t -> Dom.node Js.Nullable.t -> bool = "isTargetOnHoverCard" [@@mel.send] + external is_target_on_hover_card : t -> Dom.node Js.Null.t -> bool = "isTargetOnHoverCard" [@@mel.send] external set_trigger_ref : t -> Dom.htmlElement -> unit = "setTriggerRef" [@@mel.send] external set_content_ref : t -> Dom.htmlElement -> unit = "setContentRef" [@@mel.send] external make : dataset:(t_hover_card_data_set Solidtype.Solid.accessor [@mel.as "dataset"]) -> is_open:(bool Solidtype.Solid.accessor [@mel.as "isOpen"]) -> content_present:(bool Solidtype.Solid.accessor [@mel.as "contentPresent"]) -> open_with_delay:((unit -> unit) [@mel.as "openWithDelay"]) -> close_with_delay:((unit -> unit) [@mel.as "closeWithDelay"]) -> cancel_opening:((unit -> unit) [@mel.as "cancelOpening"]) -> cancel_closing:((unit -> unit) [@mel.as "cancelClosing"]) -> close:((unit -> unit) [@mel.as "close"]) -> is_target_on_hover_card:((Dom.node Js.Nullable.t -> bool) [@mel.as "isTargetOnHoverCard"]) -> set_trigger_ref:((Dom.htmlElement -> unit) [@mel.as "setTriggerRef"]) -> set_content_ref:((Dom.htmlElement -> unit) [@mel.as "setContentRef"]) -> unit -> t = "" [@@mel.obj] @@ -1355,7 +1355,7 @@ module TooltipContextValue : sig external cancel_opening : t -> unit -> unit = "cancelOpening" [@@mel.send] external generate_id : t -> string -> string = "generateId" [@@mel.send] val register_content_id : t -> string -> (unit -> unit) - external is_target_on_tooltip : t -> Dom.node Js.Nullable.t -> bool = "isTargetOnTooltip" [@@mel.send] + external is_target_on_tooltip : t -> Dom.node Js.Null.t -> bool = "isTargetOnTooltip" [@@mel.send] external set_trigger_ref : t -> Dom.htmlElement -> unit = "setTriggerRef" [@@mel.send] external set_content_ref : t -> Dom.htmlElement -> unit = "setContentRef" [@@mel.send] val make : dataset:t_tooltip_data_set Solidtype.Solid.accessor -> is_open:bool Solidtype.Solid.accessor -> is_disabled:bool Solidtype.Solid.accessor -> trigger_on_focus_only:bool Solidtype.Solid.accessor -> content_id:string option Solidtype.Solid.accessor -> content_present:bool Solidtype.Solid.accessor -> open_tooltip:(bool option -> unit) -> hide_tooltip:(bool option -> unit) -> cancel_opening:(unit -> unit) -> generate_id:(string -> string) -> register_content_id:(string -> (unit -> unit)) -> is_target_on_tooltip:(Dom.node Js.Nullable.t -> bool) -> set_trigger_ref:(Dom.htmlElement -> unit) -> set_content_ref:(Dom.htmlElement -> unit) -> unit -> t diff --git a/prds/heroui.md b/prds/heroui.md index 61f6082..5aabdd6 100644 --- a/prds/heroui.md +++ b/prds/heroui.md @@ -519,6 +519,9 @@ page, its line in `coverage.mdx` and `CHANGES.md`. - [x] DateRangePicker, on the headless date field's range and the headless picker, its range calendar given the picker's range - [ ] Toolbar, InputOTP +- [x] Command, beyond HeroUI's set: made headless of Kobalte's list hooks, a + search that filters a menu as React Aria's autocomplete filters it, the + focus kept in the search; its look a stylesheet of its own ## Verification diff --git a/test/does_not_compile.t b/test/does_not_compile.t index 8832707..1c00593 100644 --- a/test/does_not_compile.t +++ b/test/does_not_compile.t @@ -627,7 +627,7 @@ A single-value Select given a list for its value. > module Select = Solidtype_kobalte.Select > let make () = > value=(Js.Nullable.return [| "a" |]) /> + > value=(Js.Null.return [| "a" |]) /> > EOF $ build File "case.mlx", line 3, characters 27-66: @@ -930,3 +930,17 @@ compile. File "case.mlx", line 1, characters 29-38: Error: This pattern matches values of type int $ rm case.mlx part.mlx + +A value that may be empty is given as [Js.Null.t]: an option made one by +[Js.Nullable.fromOption], whose [None] is the undefined Kobalte reads as no +value given, does not compile. + + $ cat > case.mlx <<'EOF' + > let make () = + > + > EOF + $ build | grep -E '^File|^Error|Js.null$' + File "case.mlx", line 2, characters 44-73: + Error: This expression has type 'a Js.nullable + but an expression was expected of type string Js.null + $ rm case.mlx diff --git a/test/headless/color_picker_test.mlx b/test/headless/color_picker_test.mlx index 4184c18..bf52860 100644 --- a/test/headless/color_picker_test.mlx +++ b/test/headless/color_picker_test.mlx @@ -69,7 +69,7 @@ let () = H.render (fun () -> + value=(Js.Null.return (Colors.parse_color "#0000ff"))> ); diff --git a/test/headless/command_test.mlx b/test/headless/command_test.mlx new file mode 100644 index 0000000..442df1b --- /dev/null +++ b/test/headless/command_test.mlx @@ -0,0 +1,320 @@ +(* The headless Command as a caller meets it: a search that controls a menu of + items in groups, the focus kept in the search while the keys move through + the items shown, typing filtering them and focusing the first, Enter and a + press running one, Escape emptying the search before it goes further, its + words in the reader's language, the caller's attributes and ref kept, and no + violation axe finds. *) + +module S = Solidtype.Solid +module Jsx = Solidtype.Jsx +module Command = Solidtype_headless.Command +module I18n = Solidtype_kobalte.I18n +module E = Jest.Expect +module H = Harness +open Harness.Syntax + +external add_listener : Dom.document -> string -> (Dom.event -> unit) -> unit + = "addEventListener" +[@@mel.send] + +external document : Dom.document = "document" [@@mel.scope "globalThis"] +external key_of : Dom.event -> string = "key" [@@mel.get] +external prevented : Dom.event -> bool = "defaultPrevented" [@@mel.get] +external value : Dom.element -> string = "value" [@@mel.get] + +(* Two groups apart, "Change theme" disabled, and one item on its own, in a + palette that tells what ran. *) +let palette ?(told = ref []) ?(got_ref = ref false) ?text + ?(on_input_change = fun (_ : string) -> ()) ?filter ?locale () = + let body () = + + got_ref := true) + /> + told := ("list " ^ key) :: !told) + disabled_keys=[| "shortcuts" |] + render_empty_state=(fun () ->

"No results"

) + data_testid="list"> + + "Actions" + told := "item file" :: !told)> + "Create new file..." + + "Create new folder..." + "Assign to me" + + + + "Settings" + "Preferences" + + "Change theme" + + "Keyboard shortcuts" + + + "Help center" +
+
+ in + H.render (fun () -> + match locale with + | Some locale -> (body ()) + | None -> body ()) + +let input () = (H.all_by_role "searchbox").(0) +let items () = Array.map H.text (H.all_by_role "menuitem") + +let headings () = + Array.map + (fun g -> + match H.attribute g "aria-labelledby" with + | Some id -> ( + match Js.Null.toOption (H.by_id id) with + | Some h -> H.text h + | None -> "missing") + | None -> "none") + (H.all_by_role "group") + +let separators () = Array.length (H.all_by_role "separator") + +(* The item the search names as its active descendant. *) +let at_focus () = + match H.attribute (input ()) "aria-activedescendant" with + | Some id -> ( + match Js.Null.toOption (H.by_id id) with + | Some e -> H.text e + | None -> "missing") + | None -> "none" + +let keys user steps = + let rec go seen = function + | [] -> Js.Promise.resolve (List.rev seen) + | k :: rest -> + let* () = H.keyboard user k in + go (at_focus () :: seen) rest + in + go [] steps + +let () = + Jest.describe "Headless Command" (fun () -> + Jest.afterEach H.cleanup; + Jest.testPromise "is a search that controls a menu of groups" (fun () -> + palette (); + let menu = (H.all_by_role "menu").(0) in + let i = input () in + Js.Promise.resolve + E.( + expect + ( ( H.attribute i "aria-controls", + H.attribute i "aria-autocomplete", + H.attribute i "aria-activedescendant", + H.attribute i "aria-label" ), + H.attribute menu "aria-label", + headings (), + items (), + ( separators (), + Array.map + (fun e -> H.attribute e "aria-disabled") + (H.all_by_role "menuitem") ) ) + |> toEqual + ( ( H.attribute menu "id", + Some "list", + None, + Some "Search commands" ), + Some "Suggestions", + [| "Actions"; "Settings" |], + [| + "Create new file..."; + "Create new folder..."; + "Assign to me"; + "Preferences"; + "Change theme"; + "Keyboard shortcuts"; + "Help center"; + |], + ( 2, + [| + None; None; None; None; Some "true"; Some "true"; None; + |] ) ))); + Jest.testPromise + "is walked by the keys from the search, wrapping, disabled items passed" + (fun () -> + palette (); + let user = H.setup () in + let* () = H.click user (input ()) in + let* seen = + keys user + [ + "{ArrowDown}"; + "{ArrowDown}"; + "{ArrowUp}"; + "{ArrowUp}"; + "{ArrowUp}"; + "{ArrowUp}"; + "{ArrowDown}"; + "{Home}"; + "{End}"; + ] + in + let highlighted = + Array.map + (fun e -> H.attribute e "data-highlighted") + (H.all_by_role "menuitem") + in + Js.Promise.resolve + E.( + expect (seen, H.focused == input (), highlighted) + |> toEqual + ( [ + "Create new file..."; + "Create new folder..."; + "Create new file..."; + "Help center"; + "Preferences"; + "Assign to me"; + "Preferences"; + "Create new file..."; + "Help center"; + ], + true, + [| None; None; None; None; None; None; Some "" |] ))); + Jest.testPromise + "filters by typing, the first shown focused, the rest hidden" (fun () -> + palette (); + let user = H.setup () in + let* () = H.click user (input ()) in + let* () = H.keyboard user "fol" in + let typed = (items (), headings (), separators (), at_focus ()) in + let* () = H.keyboard user "{Backspace}" in + let deleted = at_focus () in + let* () = H.keyboard user "zz" in + let menu = (H.all_by_role "menu").(0) in + Js.Promise.resolve + E.( + expect + ( typed, + deleted, + (items (), headings (), H.attribute menu "data-empty"), + H.text menu ) + |> toEqual + ( ( [| "Create new folder..." |], + [| "Actions" |], + 0, + "Create new folder..." ), + "none", + ([| "No results" |], [||], Some ""), + "No results" ))); + Jest.testPromise "shows a separator only where it parts items shown" + (fun () -> + palette (); + let user = H.setup () in + let* () = H.click user (input ()) in + let* () = H.keyboard user "e" in + let both = separators () in + let* () = H.keyboard user "{Backspace}help" in + let alone = separators () in + let* () = H.keyboard user "{Control>}a{/Control}{Backspace}pre" in + Js.Promise.resolve + E.( + expect (both, alone, separators (), items ()) + |> toEqual (2, 0, 0, [| "Preferences" |]))); + Jest.testPromise "runs an item by Enter or a press, not a disabled one" + (fun () -> + let told = ref [] in + palette ~told (); + let user = H.setup () in + let* () = H.click user (input ()) in + let* () = H.keyboard user "{ArrowDown}{Enter}{ArrowDown}{Enter}" in + let* () = H.click user (H.by_text "Change theme") in + let* () = H.click user (H.by_text "Help center") in + Js.Promise.resolve + E.( + expect (List.rev !told, H.focused == input ()) + |> toEqual + ( [ "item file"; "list file"; "list folder"; "list help" ], + true ))); + Jest.testPromise "focuses the item the pointer is over" (fun () -> + palette (); + let user = H.setup () in + let* () = H.hover user (H.by_text "Preferences") in + let over = at_focus () in + let* () = H.hover user (H.by_text "Change theme") in + Js.Promise.resolve + E.( + expect (over, at_focus ()) + |> toEqual ("Preferences", "Preferences"))); + Jest.testPromise "empties the search by Escape before it goes further" + (fun () -> + palette (); + (* What a dialog around the palette is left: an Escape no part + handled. *) + let outside = ref 0 in + add_listener document "keydown" (fun e -> + if String.equal (key_of e) "Escape" && not (prevented e) then + incr outside); + let user = H.setup () in + let* () = H.click user (input ()) in + let* () = H.keyboard user "pre{ArrowDown}{Escape}" in + let first = (value (input ()), Array.length (items ()), !outside) in + let* () = H.keyboard user "{Escape}" in + Js.Promise.resolve + E.( + expect (first, at_focus (), !outside) + |> toEqual (("", 7, 0), "none", 1))); + Jest.testPromise "takes its text and its filter from the caller" + (fun () -> + let text, set_text = S.create_signal "" in + let asked = ref [] in + palette ~text + ~on_input_change:(fun t -> + asked := t :: !asked; + S.set set_text (String.uppercase_ascii t)) + ~filter:(fun item typed -> + String.length typed > 0 + && Char.equal item.[0] typed.[String.length typed - 1]) + (); + let user = H.setup () in + let* () = H.click user (input ()) in + let* () = H.keyboard user "xp" in + Js.Promise.resolve + E.( + expect (List.rev !asked, value (input ()), items ()) + |> toEqual ([ "x"; "Xp" ], "XP", [| "Preferences" |]))); + Jest.testPromise "speaks the reader's language" (fun () -> + palette ~locale:"de-DE" (); + Js.Promise.resolve + E.( + expect (H.attribute (H.all_by_role "menu").(0) "aria-label") + |> toEqual (Some "Empfehlungen"))); + Jest.testPromise "keeps the caller's attributes and ref" (fun () -> + let got_ref = ref false in + palette ~got_ref (); + Js.Promise.resolve + E.( + expect + ( H.attribute (input ()) "class", + H.attribute (H.all_by_role "menu").(0) "data-testid", + !got_ref ) + |> toEqual (Some "search", Some "list", true))); + Jest.testPromise "outside a palette, refuses to be made" (fun () -> + let refused = + match H.render (fun () -> ) with + | () -> "made" + | exception Invalid_argument m -> m + in + Js.Promise.resolve + E.( + expect refused + |> toEqual "Command.Input is used inside a Command.Root")); + Jest.testPromise "has no violation axe finds" (fun () -> + palette (); + let user = H.setup () in + let* () = H.click user (input ()) in + let* () = H.keyboard user "{ArrowDown}" in + let* found = H.accessibility_violations () in + Js.Promise.resolve E.(expect found |> toEqual [||]))) diff --git a/test/headless/date_field_test.mlx b/test/headless/date_field_test.mlx index 93c24b5..2af79ac 100644 --- a/test/headless/date_field_test.mlx +++ b/test/headless/date_field_test.mlx @@ -139,14 +139,10 @@ let () = E.(expect (segments ()) |> toEqual "day=21/month=11/year=2026")); Jest.test "writes the segments in the locale's order and calendar" (fun () -> - date ~locale:"de-DE" - ~value:(Js.Nullable.return (Date.make 2026 11 21)) - (); + date ~locale:"de-DE" ~value:(Js.Null.return (Date.make 2026 11 21)) (); let german = segments () in H.cleanup (); - date ~locale:"th-TH" - ~value:(Js.Nullable.return (Date.make 2026 11 21)) - (); + date ~locale:"th-TH" ~value:(Js.Null.return (Date.make 2026 11 21)) (); E.( expect (german, segments ()) |> toEqual ("day=21.month=11.year=2026", "day=21/month=11/year=2569"))); @@ -234,7 +230,7 @@ let () = [ "2026-02-28" ] ))); Jest.test "is invalid before its least date" (fun () -> date - ~value:(Js.Nullable.return (Date.make 2026 1 1)) + ~value:(Js.Null.return (Date.make 2026 1 1)) ~min_value:(Date.make 2026 6 1) (); E.( expect @@ -271,7 +267,7 @@ let () = H.cleanup (); let told = ref [] in date ~told ~is_read_only:true - ~value:(Js.Nullable.return (Date.make 2026 11 21)) + ~value:(Js.Null.return (Date.make 2026 11 21)) (); let user = H.setup () in let* () = H.click user (segment "month") in @@ -314,7 +310,7 @@ let () = |> toEqual ("2026-11-22", "2026-11-21", "day=21/month=11/year=2026"))); Jest.testPromise "has no violation axe finds" (fun () -> - date ~value:(Js.Nullable.return (Date.make 2026 11 21)) (); + date ~value:(Js.Null.return (Date.make 2026 11 21)) (); let* found = H.accessibility_violations () in Js.Promise.resolve E.(expect found |> toEqual [||]))) diff --git a/test/headless/date_picker_test.mlx b/test/headless/date_picker_test.mlx index 269369b..ce30252 100644 --- a/test/headless/date_picker_test.mlx +++ b/test/headless/date_picker_test.mlx @@ -138,6 +138,26 @@ let () = "11 9 2026", None, true ))); + Jest.testPromise + "is emptied by a null value after a date is typed and told" (fun () -> + let value, set_value = S.create_signal Js.Null.empty in + H.render (fun () -> + + S.set set_value (Js.Null.fromOption d))> + (inside ()) + + ); + let user = H.setup () in + let* () = H.click user (one "[role=spinbutton]") in + let* () = H.keyboard user "11212026" in + let typed = segments () in + S.set set_value Js.Null.empty; + let* () = H.frame () in + Js.Promise.resolve + E.( + expect (typed, segments ()) |> toEqual ("11 21 2026", "mm dd yyyy"))); Jest.testPromise "closes by Escape, its value kept" (fun () -> let told = ref [] in picker ~told (); diff --git a/test/headless/date_range_field_test.mlx b/test/headless/date_range_field_test.mlx index 83c2244..4ef1f68 100644 --- a/test/headless/date_range_field_test.mlx +++ b/test/headless/date_range_field_test.mlx @@ -123,6 +123,34 @@ let () = ( [], "2026-11-03..2026-11-06", [ "from=2026-11-03"; "to=2026-11-06" ] ))); + Jest.testPromise + "moves the focus between its ends by the keys and as an end is typed" + (fun () -> + range (); + let user = H.setup () in + let focused () = H.attribute H.focused "data-type" in + let at_start () = + Js.Array.includes ~value:H.focused + (Js.Array.from (query_all (inputs ()).(0) "[role=spinbutton]")) + in + let* () = H.click user (segment 0 "year") in + let* () = H.keyboard user "{ArrowRight}" in + let right = (focused (), at_start ()) in + let* () = H.keyboard user "{ArrowLeft}" in + let left = (focused (), at_start ()) in + let* () = H.click user (segment 1 "month") in + let* () = H.keyboard user "{Backspace}" in + let erased = (focused (), at_start ()) in + let* () = H.click user (segment 0 "month") in + let* () = H.keyboard user "11032026" in + Js.Promise.resolve + E.( + expect (right, left, erased, (focused (), at_start ())) + |> toEqual + ( (Some "month", false), + (Some "year", true), + (Some "year", true), + (Some "month", false) ))); Jest.testPromise "tells None as an end is erased whole" (fun () -> let told = ref [] in range ~told diff --git a/test/headless/kobalte_fixes_test.mlx b/test/headless/kobalte_fixes_test.mlx index f3b9626..3cb7975 100644 --- a/test/headless/kobalte_fixes_test.mlx +++ b/test/headless/kobalte_fixes_test.mlx @@ -408,6 +408,27 @@ let handled ~fixed = Js.Promise.resolve (H.attribute input "aria-valuenow", List.sort String.compare !told) +external input_value : Dom.element -> string = "value" [@@mel.get] + +(* What a number field, the fixed one or Kobalte's own, shows given a number + and then not a number, which React Aria shows as empty. *) +let emptied ~fixed = + let raw, set_raw = S.create_signal 3. in + H.render (fun () -> + if fixed then + + + + else + + + ); + let input = query "[role=spinbutton]" in + let given = input_value input in + S.set set_raw Float.nan; + let* () = H.frame () in + Js.Promise.resolve (given, input_value input) + (* The hue a channel field, the fixed one or Kobalte's own, tells after a step up by the keys, and whether its caller's key handler was called. *) let channel_handled ~fixed = @@ -445,6 +466,13 @@ let () = |> toEqual ( (Some "5", [ "blur"; "focus"; "key" ]), (Some "3", [ "blur"; "focus"; "key" ]) ))); + Jest.testPromise "is emptied by a raw value that is not a number" + (fun () -> + let* fixed = emptied ~fixed:true in + H.cleanup (); + let* kobalte = emptied ~fixed:false in + Js.Promise.resolve + E.(expect (fixed, kobalte) |> toEqual (("3", ""), ("3", "3")))); Jest.testPromise "steps a colour's channel by the keys, its caller's key handler called" (fun () -> diff --git a/test/heroui/attributes_test.mlx b/test/heroui/attributes_test.mlx index 276ab65..8c33720 100644 --- a/test/heroui/attributes_test.mlx +++ b/test/heroui/attributes_test.mlx @@ -20,6 +20,7 @@ module Drawer = H.Drawer module Dropdown = H.Dropdown module Header = H.Header module Modal = H.Modal +module Command = H.Command module Pagination = H.Pagination module Popover = H.Popover module Tooltip = H.Tooltip @@ -1155,6 +1156,164 @@ let () = + + seen "Command.Backdrop" e) + on_key_down=(fun e -> heard "Command.Backdrop" e)> + seen "Command.Container" e) + on_key_down=(fun e -> heard "Command.Container" e)> + seen "Command.Dialog" e) + on_key_down=(fun e -> heard "Command.Dialog" e)> + seen "Command.Header" e) + on_key_down=(fun e -> heard "Command.Header" e)> + "Home" + + seen "Command.Input_group" e) + on_key_down=(fun e -> + heard "Command.Input_group" e)> + + seen "Command.Input_group.Prefix" e) + on_key_down=(fun e -> + heard "Command.Input_group.Prefix" + e)> + "?" + + + seen "Command.Input_group.Input" e) + on_key_down=(fun e -> + heard "Command.Input_group.Input" + e) + /> + + seen "Command.Input_group.Clear_button" e) + on_key_down=(fun e -> + heard + "Command.Input_group.Clear_button" + e) + /> + + seen "Command.Input_group.Suffix" e) + on_key_down=(fun e -> + heard "Command.Input_group.Suffix" + e)> + "Esc" + + + seen "Command.List" e) + on_key_down=(fun e -> heard "Command.List" e)> + seen "Command.Group" e) + on_key_down=(fun e -> heard "Command.Group" e)> + seen "Command.Item" e) + on_key_down=(fun e -> heard "Command.Item" e)> + "Open" + + + seen "Command.Separator" e) + on_key_down=(fun e -> + heard "Command.Separator" e) + /> + + "Theme" + + + seen "Command.Footer" e) + on_key_down=(fun e -> heard "Command.Footer" e)> + "Navigate" + + + + + string = "value" [@@mel.get] + +external text_by : string -> Dom.element = "getByText" +[@@mel.module "@solidjs/testing-library"] [@@mel.scope "screen"] + +let all selector = Js.Array.from (T.all_with selector) + +let one selector = + match T.query selector with + | Some e -> e + | None -> Js.Exn.raiseError ("nothing is " ^ selector) + +let slot name = one ("[data-slot=" ^ name ^ "]") +let classes name = T.class_name (slot name) + +(* A while for Kobalte's focus moves and the overlay's timers to run. *) +let settle () = T.wait 30 + +let palette ?(variant = `opaque) ?(size = `md) ?(told = ref []) () = + T.render (fun () -> + + + + + + "Home" + + "?" + + + "Esc" + + told := key :: !told) + render_empty_state=(fun () -> Jsx.string "No results found.")> + + + "Create new file..." + + + "Create new folder..." + + + + + "Change theme" + + + "Navigate" + + + + ) + +let open_palette user = + let* () = T.click user (T.by_role "button") in + settle () + +let () = + Jest.describe "HeroUI Command" (fun () -> + Jest.afterEach T.cleanup; + Jest.testPromise "opens by its trigger, the focus in its search" + (fun () -> + palette (); + let closed = T.query "[role=dialog]" in + let user = T.setup () in + let* () = open_palette user in + let dialog = one "[role=dialog]" in + Js.Promise.resolve + E.( + expect + ( Option.is_some closed, + T.attribute dialog "aria-label", + active == slot "command-input-group-input", + T.attribute active "placeholder", + T.attribute active "aria-label" ) + |> toEqual + ( false, + Some "Command palette", + true, + Some "Type a command or search...", + Some "Search commands" ))); + Jest.testPromise "draws each part with its classes and slot" (fun () -> + palette ~variant:`blur ~size:`lg (); + let user = T.setup () in + let* () = open_palette user in + Js.Promise.resolve + E.( + expect + [| + classes "command-backdrop"; + classes "command-container"; + classes "command-dialog"; + classes "command-header"; + classes "command-input-group"; + classes "command-input-group-prefix"; + classes "command-input-group-input"; + classes "command-input-group-clear-button"; + classes "command-input-group-suffix"; + classes "command-list"; + classes "command-group"; + classes "command-group-heading"; + T.class_name (one "[data-key=file]"); + T.class_name (one "[data-slot=command-separator]"); + classes "command-footer"; + |] + |> toEqual + [| + "command__backdrop command__backdrop--blur"; + "command__container"; + "command__dialog command__dialog--lg"; + "command__header"; + "command__input-group"; + "command__input-group-prefix"; + "command__input-group-input"; + "close-button close-button--default \ + command__input-group-clear-button"; + "command__input-group-suffix"; + "command__list"; + "command__group"; + "command__group-heading"; + "command__item first"; + "command__separator"; + "command__footer"; + |])); + Jest.testPromise "shows its items' state as its styles read it" (fun () -> + palette (); + let user = T.setup () in + let* () = open_palette user in + let* () = T.keyboard user "{ArrowDown}" in + let state e = + ( T.attribute e "data-focused", + T.attribute e "data-disabled", + T.attribute e "data-hovered" ) + in + let focused = Array.map state (all "[role=menuitem]") in + let* () = T.hover user (text_by "Change theme") in + Js.Promise.resolve + E.( + expect + ( focused, + state (text_by "Change theme"), + T.attribute (slot "command-input-group") "data-empty" ) + |> toEqual + ( [| + (Some "true", None, None); + (None, Some "true", None); + (None, None, None); + |], + (Some "true", None, Some "true"), + Some "true" ))); + Jest.testPromise "is emptied by its clear button, the search refocused" + (fun () -> + palette (); + let user = T.setup () in + let* () = open_palette user in + let* () = T.keyboard user "the" in + let typed = + ( T.attribute (slot "command-input-group") "data-empty", + Array.length (all "[role=menuitem]") ) + in + let clear = slot "command-input-group-clear-button" in + let* () = T.click user clear in + Js.Promise.resolve + E.( + expect + ( typed, + T.attribute clear "aria-label", + T.attribute clear "tabindex", + value (slot "command-input-group-input"), + active == slot "command-input-group-input", + Array.length (all "[role=menuitem]") ) + |> toEqual ((None, 1), Some "Clear search", Some "-1", "", true, 3))); + Jest.testPromise + "runs an item by Enter and stays open; Escape empties, then closes" + (fun () -> + let told = ref [] in + palette ~told (); + let user = T.setup () in + let* () = open_palette user in + let* () = T.keyboard user "the{Enter}" in + let ran = (!told, Option.is_some (T.query "[role=dialog]")) in + let* () = T.keyboard user "{Escape}" in + let* () = settle () in + let emptied = + ( value (slot "command-input-group-input"), + Option.is_some (T.query "[role=dialog]") ) + in + let* () = T.keyboard user "{Escape}" in + let* () = T.wait 250 in + Js.Promise.resolve + E.( + expect + ( ran, + emptied, + Option.is_some (T.query "[role=dialog]"), + active == T.by_role "button" ) + |> toEqual (([ "theme" ], true), ("", true), false, true))); + Jest.testPromise "draws its empty state while nothing is shown" (fun () -> + palette (); + let user = T.setup () in + let* () = open_palette user in + let* () = T.keyboard user "zz" in + let empty = slot "command-empty" in + Js.Promise.resolve + E.( + expect + ( T.class_name empty, + T.text_content empty, + T.attribute (slot "command-list") "data-empty", + Option.is_some (T.query "[role=group]") ) + |> toEqual ("command__empty", "No results found.", Some "", false))); + Jest.testPromise "has no violation axe finds" (fun () -> + palette (); + let user = T.setup () in + let* () = open_palette user in + let* () = T.keyboard user "{ArrowDown}" in + let* found = T.accessibility_violations () in + Js.Promise.resolve E.(expect found |> toEqual [||]))) diff --git a/test/heroui/controlled_empty_test.mlx b/test/heroui/controlled_empty_test.mlx new file mode 100644 index 0000000..93daf0d --- /dev/null +++ b/test/heroui/controlled_empty_test.mlx @@ -0,0 +1,288 @@ +(* Each port whose value can be empty, controlled empty: a value entered and + told, then the value set empty again, which empties it, as React's does. + Its value given as [Js.Null.t], an option is made one only so. *) + +module S = Solidtype.Solid +module Jsx = Solidtype.Jsx +module Calendar = Solidtype_heroui.Calendar +module Range_calendar = Solidtype_heroui.Range_calendar +module Color_field = Solidtype_heroui.Color_field +module Date_field = Solidtype_heroui.Date_field +module Date_picker = Solidtype_heroui.Date_picker +module Date_range_picker = Solidtype_heroui.Date_range_picker +module Time_field = Solidtype_heroui.Time_field +module Radio_group = Solidtype_heroui.Radio_group +module Radio = Solidtype_heroui.Radio +module Number_field = Solidtype_heroui.Number_field +module Select = Solidtype_heroui.Select +module Combo_box = Solidtype_heroui.Combo_box +module List_box = Solidtype_heroui.List_box +module Input = Solidtype_heroui.Input +module Label = Solidtype_heroui.Label +module I18n = Solidtype_kobalte.I18n +module D = Solidtype_internationalized_date +module E = Jest.Expect +module T = Testing +open Testing.Syntax + +external value_of : Dom.element -> string = "value" [@@mel.get] +external checked : Dom.element -> bool = "checked" [@@mel.get] + +let all selector = Js.Array.from (T.all_with selector) + +let digits () = + all "[role=spinbutton]" |> Array.map T.text_content |> Array.to_list + |> String.concat "" |> String.to_seq + |> Seq.filter (fun c -> c >= '0' && c <= '9') + |> String.of_seq + +let value selector = + match T.query selector with Some e -> value_of e | None -> "?" + +let is_checked selector = + match T.query selector with Some e -> checked e | None -> false + +let click selector = + match T.query selector with + | Some e -> T.click (T.setup ()) e + | None -> Js.Promise.resolve () + +let nullable = function Some v -> Js.Null.return v | None -> Js.Null.empty + +let in_locale f = + T.render (fun () -> + (f ())) + +let type_in selector text = + let* () = click selector in + let* () = T.keyboard (T.setup ()) text in + T.frame () + +let click_day n = + match + all "[role=gridcell] *" |> Array.to_list + |> List.find_opt (fun e -> + String.equal (String.trim (T.text_content e)) (string_of_int n)) + with + | Some cell -> T.click (T.setup ()) cell + | None -> Js.Promise.resolve () + +let selected () = + string_of_int (Array.length (all "[role=gridcell][aria-selected=true]")) + +(* Controlled empty, a value entered and stored, then the value set empty + again: what the field shows then is what it showed before any input. *) +let check name ~render ~enter ~shown = + Jest.testPromise name (fun () -> + let value, set_value = S.create_signal Js.Null.empty in + render value (fun v -> S.set set_value (nullable v)); + let* () = T.frame () in + let empty = shown () in + let* () = enter () in + let entered = shown () in + S.set set_value Js.Null.empty; + let* () = T.frame () in + Js.Promise.resolve + E.( + expect (not (String.equal entered empty), shown ()) + |> toEqual (true, empty))) + +let () = + Jest.describe "a controlled value set empty after input" (fun () -> + Jest.afterEach T.cleanup; + check "Date_field" + ~render:(fun v set -> + in_locale (fun () -> + + + + + (fun segment -> + ) + + + )) + ~enter:(fun () -> type_in "[role=spinbutton]" "11212026") + ~shown:(fun () -> digits () ^ "|" ^ value "input[name=d]"); + check "Time_field" + ~render:(fun v set -> + in_locale (fun () -> + + + + + (fun segment -> + ) + + + )) + ~enter:(fun () -> type_in "[role=spinbutton]" "0930a") + ~shown:(fun () -> digits () ^ "|" ^ value "input[name=t]"); + check "Date_picker" + ~render:(fun v set -> + in_locale (fun () -> + + + + + (fun segment -> + ) + + + )) + ~enter:(fun () -> type_in "[role=spinbutton]" "11212026") + ~shown:(fun () -> digits () ^ "|" ^ value "input[name=d]"); + check "Date_range_picker" + ~render:(fun v set -> + in_locale (fun () -> + + + + + (fun segment -> + ) + + + (fun segment -> + ) + + + )) + ~enter:(fun () -> type_in "[role=spinbutton]" "1121202611232026") + ~shown:(fun () -> + digits () ^ "|" ^ value "input[name=s]" ^ "/" ^ value "input[name=e]"); + check "Calendar" + ~render:(fun v set -> + in_locale (fun () -> + set (Some d))> + + + (fun date -> ) + + + )) + ~enter:(fun () -> click_day 21) + ~shown:selected; + check "Range_calendar" + ~render:(fun v set -> + in_locale (fun () -> + + + + (fun date -> ) + + + )) + ~enter:(fun () -> + let* () = click_day 21 in + click_day 23) + ~shown:selected; + check "Color_field" + ~render:(fun v set -> + T.render (fun () -> + + + + )) + ~enter:(fun () -> + type_in "[data-slot=color-field] input:not([type=hidden])" + "#ff0000{Enter}") + ~shown:(fun () -> + value "[data-slot=color-field] input:not([type=hidden])" + ^ "|" ^ value "input[name=c]"); + Jest.testPromise "Radio_group" (fun () -> + let v, set_v = S.create_signal Js.Null.empty in + T.render (fun () -> + S.set set_v (Js.Null.return x))> + + "A" + "B" + ); + let* () = click "input[value=b]" in + let* () = T.frame () in + let picked = is_checked "input[value=b]" in + S.set set_v Js.Null.empty; + let* () = T.frame () in + Js.Promise.resolve + E.( + expect (picked, is_checked "input[value=b]") + |> toEqual (true, false))); + Jest.testPromise "Number_field, set to NaN" (fun () -> + let v, set_v = S.create_signal Float.nan in + T.render (fun () -> + + + + ); + let* () = type_in "input[inputmode]" "42{Enter}" in + let entered = value "input[inputmode]" in + S.set set_v Float.nan; + let* () = T.frame () in + Js.Promise.resolve + E.(expect (entered, value "input[inputmode]") |> toEqual ("42", ""))); + Jest.testPromise "Select, set to no keys" (fun () -> + let v, set_v = S.create_signal [| "b" |] in + T.render (fun () -> + ); + let* () = T.frame () in + let shown () = + match T.query "[data-slot=select-value]" with + | Some e -> T.text_content e + | None -> "?" + in + let before = shown () in + S.set set_v [||]; + let* () = T.frame () in + Js.Promise.resolve + E.(expect (before, shown ()) |> toEqual ("Beta", "Pick"))); + Jest.testPromise "Combo_box, set to no keys" (fun () -> + let v, set_v = S.create_signal [| "b" |] in + T.render (fun () -> + + + + + + + "Alpha" + + + "Beta" + + + + ); + let* () = T.frame () in + let before = value "input[role=combobox]" in + S.set set_v [||]; + let* () = T.frame () in + Js.Promise.resolve + E.( + expect (before, value "input[role=combobox]") + |> toEqual ("Beta", "")))) diff --git a/test/heroui/date_range_picker_test.mlx b/test/heroui/date_range_picker_test.mlx index 650c9cb..fd5c9b4 100644 --- a/test/heroui/date_range_picker_test.mlx +++ b/test/heroui/date_range_picker_test.mlx @@ -253,6 +253,41 @@ let same_but_invalid_at_once (solidtype, react) = let same (solidtype, react) = Js.Promise.resolve E.(expect solidtype |> toEqual react) +external active : Dom.element Js.Nullable.t = "activeElement" +[@@mel.scope "document"] + +external closest : Dom.element -> string -> Dom.element Js.Nullable.t + = "closest" +[@@mel.send] + +(* Where the keys move the focus from the start's last segment and the end's + first: the focused segment's text, and which end it is in. *) +let moves step = + let where () = + match Js.Nullable.toOption active with + | Some a -> ( + T.text_content a ^ " in " + ^ + match Js.Nullable.toOption (closest a "[data-slot=date-input]") with + | Some s -> Option.value (T.attribute s "slot") ~default:"?" + | None -> "?") + | None -> "nothing" + in + let user = T.setup () in + let segment i = (Js.Array.from (T.all_with "[role=spinbutton]")).(i) in + let* () = step (fun () -> T.click user (segment 2)) in + let* () = step (fun () -> T.keyboard user "{ArrowRight}") in + let right = where () in + let* () = step (fun () -> T.click user (segment 3)) in + let* () = step (fun () -> T.keyboard user "{ArrowLeft}") in + let left = where () in + let* () = step (fun () -> T.click user (segment 3)) in + let* () = step (fun () -> T.keyboard user "{Backspace}") in + let erased = where () in + let* () = step (fun () -> T.click user (segment 0)) in + let* () = step (fun () -> T.keyboard user "11212026") in + Js.Promise.resolve (right, left, erased, where ()) + let () = Jest.describe "Date_range_picker" (fun () -> Jest.afterEach T.cleanup; @@ -294,4 +329,32 @@ let () = ~default_value:((2026, 11, 21), (2026, 11, 23)) () in - same r)) + same r); + Jest.testPromise + "moves through both ends by the keys and as a segment is typed, as \ + React's does" (fun () -> + let (_ : Dom.element) = + Twin.render (props ~locale:"en-US" ()) (fun _ -> ()) + in + let* react = moves Twin.step in + Twin.cleanup (); + let* () = T.frame () in + T.render (fun () -> + + + + + + (fun segment -> + ) + + + + (fun segment -> + ) + + + + ); + let* solidtype = moves (fun f -> f ()) in + Js.Promise.resolve E.(expect solidtype |> toEqual react))) diff --git a/test/heroui/number_field_test.mlx b/test/heroui/number_field_test.mlx index 79402b1..346119c 100644 --- a/test/heroui/number_field_test.mlx +++ b/test/heroui/number_field_test.mlx @@ -418,6 +418,42 @@ let stepped_by_buttons ~named = Twin.cleanup (); Js.Promise.resolve ((solidtype_named, solidtype), (react_named, react)) +(* What a field, beginning at 4, tells as a number is typed, then committed by + Enter; then stepped by the keys after more is typed; then typed again and + left; then stepped after a key the field refuses: each step's changes + told, and what it shows last. *) +let commits ~render ~step = + let told = ref [] in + let tell v = told := Js.Float.toString v :: !told in + let (_ : Dom.element) = render tell in + let input () = + match Js.Null.toOption (query T.body "input[inputmode]") with + | Some i -> i + | None -> T.body + in + let user = T.setup () in + let changes () = + let c = String.concat "," (List.rev !told) in + told := []; + c + in + let* () = step (fun () -> T.click user (input ())) in + let* () = step (fun () -> T.keyboard user "{Backspace}42") in + let typed = changes () in + let* () = step (fun () -> T.keyboard user "{Enter}") in + let entered = changes () in + let* () = + step (fun () -> T.keyboard user "{Backspace}{Backspace}9{ArrowUp}") + in + let stepped = changes () in + let* () = step (fun () -> T.keyboard user "{Backspace}{Backspace}7") in + let* () = step (fun () -> T.tab user) in + let left = changes () in + let* () = step (fun () -> T.click user (input ())) in + let* () = step (fun () -> T.keyboard user "x{ArrowUp}") in + Js.Promise.resolve + (typed, entered, stepped, left, changes (), value_of (input ())) + let () = Jest.describe "Number_field" (fun () -> Jest.testPromise @@ -460,6 +496,29 @@ let () = in Js.Promise.resolve E.(expect solidtype |> toEqual react)); Jest.afterEach T.cleanup; + Jest.testPromise + "tells typed text once committed, by Enter or as the focus leaves, as \ + React's does" (fun () -> + let* react = + commits + ~render:(fun tell -> Twin.render (props ()) tell) + ~step:Twin.step + in + Twin.cleanup (); + let* solidtype = + commits + ~render:(fun tell -> + T.render (fun () -> + + + + + + ); + T.body) + ~step:(fun f -> f ()) + in + Js.Promise.resolve E.(expect solidtype |> toEqual react)); Jest.testPromise "steps its value, as React's does" (fun () -> let* r = both () in same r); diff --git a/test/heroui/time_field_test.mlx b/test/heroui/time_field_test.mlx index 906c1f5..539eb41 100644 --- a/test/heroui/time_field_test.mlx +++ b/test/heroui/time_field_test.mlx @@ -487,14 +487,14 @@ let () = Jest.testPromise "is typed onto its placeholder's date and zone, empty" (fun () -> let told = ref "" in - let value, set_value = S.create_signal Js.Nullable.null in + let value, set_value = S.create_signal Js.Null.empty in render_en_us (fun () -> - S.set set_value (Js.Nullable.fromOption v); + S.set set_value (Js.Null.fromOption v); Option.iter (fun v -> told := Zoned.to_string v ()) v)> @@ -545,19 +545,19 @@ let () = Jest.afterEach T.cleanup; Jest.testPromise "is typed into, then emptied by the null it is controlled by" (fun () -> - let time, set_time = S.create_signal Js.Nullable.null in + let time, set_time = S.create_signal Js.Null.empty in render_en_us (fun () -> S.set set_time (Js.Nullable.fromOption t))> + on_change=(fun t -> S.set set_time (Js.Null.fromOption t))> (inside ()) ); let user = T.setup () in let* () = T.click user (segment T.body "hour") in let* () = T.keyboard user "0930" in let typed = placeholder "minute" in - S.set set_time Js.Nullable.null; + S.set set_time Js.Null.empty; Js.Promise.resolve E.( expect (typed, placeholder "hour", placeholder "minute") diff --git a/vite-plugin/test/pack.test.ts b/vite-plugin/test/pack.test.ts index f72bc03..128b61e 100644 --- a/vite-plugin/test/pack.test.ts +++ b/vite-plugin/test/pack.test.ts @@ -43,7 +43,7 @@ test("the release's plugin gives an application the extras' stylesheet, in the c .filter((file) => file.endsWith(".css")) .map((file) => readFileSync(join(assets, file), "utf8")) .join("\n"); - for (const block of [".segment", ".trend-chip__indicator", ".text-shimmer"]) { + for (const block of [".segment", ".trend-chip__indicator", ".text-shimmer", ".command__dialog"]) { const at = css.indexOf(` ${block} {`); expect(at, `${block} is in the stylesheet`).toBeGreaterThan(-1); const layer = css.slice(0, at).match(/@layer (\w+) \{(?![\s\S]*@layer \w+ \{)/)?.[1];