@contui/ui 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +30 -0
- package/README.md +150 -2
- package/cli/contui.mjs +250 -0
- package/dist/charts/area-chart.d.ts +8 -0
- package/dist/charts/area-chart.js +9 -0
- package/dist/charts/bar-chart.d.ts +9 -0
- package/dist/charts/bar-chart.js +10 -0
- package/dist/charts/bubble-chart.d.ts +60 -0
- package/dist/charts/bubble-chart.js +392 -0
- package/dist/charts/bullet-chart.d.ts +53 -0
- package/dist/charts/bullet-chart.js +123 -0
- package/dist/charts/candlestick-chart.d.ts +77 -0
- package/dist/charts/candlestick-chart.js +558 -0
- package/dist/charts/cartesian.d.ts +121 -0
- package/dist/charts/cartesian.js +625 -0
- package/dist/charts/chart-card.d.ts +80 -0
- package/dist/charts/chart-card.js +114 -0
- package/dist/charts/donut-chart.d.ts +40 -0
- package/dist/charts/donut-chart.js +88 -0
- package/dist/charts/funnel-chart.d.ts +16 -0
- package/dist/charts/funnel-chart.js +164 -0
- package/dist/charts/horizontal-bar-chart.d.ts +67 -0
- package/dist/charts/horizontal-bar-chart.js +481 -0
- package/dist/charts/index.d.ts +21 -0
- package/dist/charts/index.js +32 -0
- package/dist/charts/legend.d.ts +23 -0
- package/dist/charts/legend.js +22 -0
- package/dist/charts/lib/candlestick-window.d.ts +15 -0
- package/dist/charts/lib/candlestick-window.js +23 -0
- package/dist/charts/lib/color.d.ts +39 -0
- package/dist/charts/lib/color.js +83 -0
- package/dist/charts/lib/format.d.ts +11 -0
- package/dist/charts/lib/format.js +33 -0
- package/dist/charts/lib/frame.d.ts +6 -0
- package/dist/charts/lib/frame.js +6 -0
- package/dist/charts/lib/layout.d.ts +11 -0
- package/dist/charts/lib/layout.js +71 -0
- package/dist/charts/lib/path.d.ts +35 -0
- package/dist/charts/lib/path.js +203 -0
- package/dist/charts/lib/scale.d.ts +41 -0
- package/dist/charts/lib/scale.js +80 -0
- package/dist/charts/lib/use-active.d.ts +18 -0
- package/dist/charts/lib/use-active.js +71 -0
- package/dist/charts/lib/use-size.d.ts +9 -0
- package/dist/charts/lib/use-size.js +40 -0
- package/dist/charts/line-chart.d.ts +7 -0
- package/dist/charts/line-chart.js +8 -0
- package/dist/charts/radar-chart.d.ts +27 -0
- package/dist/charts/radar-chart.js +165 -0
- package/dist/charts/rose-chart.d.ts +35 -0
- package/dist/charts/rose-chart.js +242 -0
- package/dist/charts/sparkline.d.ts +35 -0
- package/dist/charts/sparkline.js +110 -0
- package/dist/charts/tooltip.d.ts +35 -0
- package/dist/charts/tooltip.js +22 -0
- package/dist/charts/treemap.d.ts +28 -0
- package/dist/charts/treemap.js +103 -0
- package/dist/charts/uptime-strip.d.ts +40 -0
- package/dist/charts/uptime-strip.js +178 -0
- package/dist/components/accordion.d.ts +31 -0
- package/dist/components/accordion.js +34 -0
- package/dist/components/activity-item.d.ts +12 -0
- package/dist/components/activity-item.js +6 -0
- package/dist/components/alert.d.ts +21 -0
- package/dist/components/alert.js +58 -0
- package/dist/components/assistant-answer.d.ts +52 -0
- package/dist/components/assistant-answer.js +40 -0
- package/dist/components/assistant-prompt.d.ts +63 -0
- package/dist/components/assistant-prompt.js +122 -0
- package/dist/components/avatar.d.ts +28 -0
- package/dist/components/avatar.js +75 -0
- package/dist/components/badge.d.ts +16 -0
- package/dist/components/badge.js +33 -0
- package/dist/components/bar-list.d.ts +65 -0
- package/dist/components/bar-list.js +119 -0
- package/dist/components/block-slider.d.ts +43 -0
- package/dist/components/block-slider.js +63 -0
- package/dist/components/branch-graph.d.ts +87 -0
- package/dist/components/branch-graph.js +231 -0
- package/dist/components/breadcrumb.d.ts +41 -0
- package/dist/components/breadcrumb.js +53 -0
- package/dist/components/button.d.ts +23 -0
- package/dist/components/button.js +108 -0
- package/dist/components/calendar.d.ts +5 -0
- package/dist/components/calendar.js +90 -0
- package/dist/components/card.d.ts +65 -0
- package/dist/components/card.js +104 -0
- package/dist/components/chat-attachment.d.ts +47 -0
- package/dist/components/chat-attachment.js +63 -0
- package/dist/components/chat-event.d.ts +7 -0
- package/dist/components/chat-event.js +7 -0
- package/dist/components/chat-message.d.ts +59 -0
- package/dist/components/chat-message.js +91 -0
- package/dist/components/chat-reference.d.ts +20 -0
- package/dist/components/chat-reference.js +41 -0
- package/dist/components/chat-shared.d.ts +21 -0
- package/dist/components/chat-shared.js +23 -0
- package/dist/components/chat-suggestions.d.ts +59 -0
- package/dist/components/chat-suggestions.js +88 -0
- package/dist/components/chat-thread.d.ts +39 -0
- package/dist/components/chat-thread.js +172 -0
- package/dist/components/chat-typing.d.ts +35 -0
- package/dist/components/chat-typing.js +66 -0
- package/dist/components/checkbox.d.ts +18 -0
- package/dist/components/checkbox.js +33 -0
- package/dist/components/checklist.d.ts +26 -0
- package/dist/components/checklist.js +18 -0
- package/dist/components/chip.d.ts +29 -0
- package/dist/components/chip.js +61 -0
- package/dist/components/code-input.d.ts +40 -0
- package/dist/components/code-input.js +285 -0
- package/dist/components/color-picker.d.ts +38 -0
- package/dist/components/color-picker.js +855 -0
- package/dist/components/combobox.d.ts +75 -0
- package/dist/components/combobox.js +112 -0
- package/dist/components/command-menu.d.ts +58 -0
- package/dist/components/command-menu.js +166 -0
- package/dist/components/copy-button.d.ts +16 -0
- package/dist/components/copy-button.js +31 -0
- package/dist/components/data-grid.d.ts +50 -0
- package/dist/components/data-grid.js +599 -0
- package/dist/components/date-picker.d.ts +109 -0
- package/dist/components/date-picker.js +268 -0
- package/dist/components/description-list.d.ts +12 -0
- package/dist/components/description-list.js +10 -0
- package/dist/components/dialog.d.ts +57 -0
- package/dist/components/dialog.js +140 -0
- package/dist/components/dock.d.ts +45 -0
- package/dist/components/dock.js +79 -0
- package/dist/components/drop-zone.d.ts +46 -0
- package/dist/components/drop-zone.js +101 -0
- package/dist/components/dropdown-menu.d.ts +50 -0
- package/dist/components/dropdown-menu.js +78 -0
- package/dist/components/editable-cell.d.ts +34 -0
- package/dist/components/editable-cell.js +89 -0
- package/dist/components/empty-state.d.ts +17 -0
- package/dist/components/empty-state.js +9 -0
- package/dist/components/field.d.ts +43 -0
- package/dist/components/field.js +75 -0
- package/dist/components/filter-condition.d.ts +188 -0
- package/dist/components/filter-condition.js +113 -0
- package/dist/components/filter-menu.d.ts +57 -0
- package/dist/components/filter-menu.js +46 -0
- package/dist/components/header-bar.d.ts +84 -0
- package/dist/components/header-bar.js +94 -0
- package/dist/components/hold-button.d.ts +15 -0
- package/dist/components/hold-button.js +88 -0
- package/dist/components/icon-tile.d.ts +12 -0
- package/dist/components/icon-tile.js +29 -0
- package/dist/components/info-tip.d.ts +13 -0
- package/dist/components/info-tip.js +26 -0
- package/dist/components/input.d.ts +17 -0
- package/dist/components/input.js +83 -0
- package/dist/components/kbd.d.ts +6 -0
- package/dist/components/kbd.js +7 -0
- package/dist/components/menu-item.d.ts +56 -0
- package/dist/components/menu-item.js +94 -0
- package/dist/components/message-composer.d.ts +75 -0
- package/dist/components/message-composer.js +199 -0
- package/dist/components/nav-rail.d.ts +31 -0
- package/dist/components/nav-rail.js +85 -0
- package/dist/components/notice.d.ts +45 -0
- package/dist/components/notice.js +66 -0
- package/dist/components/optimistic-undo.d.ts +28 -0
- package/dist/components/optimistic-undo.js +43 -0
- package/dist/components/option-row.d.ts +15 -0
- package/dist/components/option-row.js +14 -0
- package/dist/components/page-content.d.ts +11 -0
- package/dist/components/page-content.js +31 -0
- package/dist/components/pagination.d.ts +34 -0
- package/dist/components/pagination.js +41 -0
- package/dist/components/popover.d.ts +27 -0
- package/dist/components/popover.js +25 -0
- package/dist/components/progress.d.ts +33 -0
- package/dist/components/progress.js +75 -0
- package/dist/components/radio-group.d.ts +7 -0
- package/dist/components/radio-group.js +33 -0
- package/dist/components/readonly-field.d.ts +33 -0
- package/dist/components/readonly-field.js +80 -0
- package/dist/components/segmented-control.d.ts +21 -0
- package/dist/components/segmented-control.js +108 -0
- package/dist/components/select.d.ts +42 -0
- package/dist/components/select.js +86 -0
- package/dist/components/separator.d.ts +10 -0
- package/dist/components/separator.js +7 -0
- package/dist/components/setting-row.d.ts +53 -0
- package/dist/components/setting-row.js +93 -0
- package/dist/components/sidebar-switcher.d.ts +47 -0
- package/dist/components/sidebar-switcher.js +20 -0
- package/dist/components/sidebar.d.ts +220 -0
- package/dist/components/sidebar.js +327 -0
- package/dist/components/skeleton.d.ts +18 -0
- package/dist/components/skeleton.js +23 -0
- package/dist/components/slider.d.ts +15 -0
- package/dist/components/slider.js +28 -0
- package/dist/components/spinner.d.ts +14 -0
- package/dist/components/spinner.js +19 -0
- package/dist/components/status-dot.d.ts +24 -0
- package/dist/components/status-dot.js +35 -0
- package/dist/components/stepper.d.ts +60 -0
- package/dist/components/stepper.js +197 -0
- package/dist/components/switch.d.ts +9 -0
- package/dist/components/switch.js +26 -0
- package/dist/components/table-cell.d.ts +38 -0
- package/dist/components/table-cell.js +107 -0
- package/dist/components/table-context.d.ts +48 -0
- package/dist/components/table-context.js +69 -0
- package/dist/components/table-head.d.ts +42 -0
- package/dist/components/table-head.js +188 -0
- package/dist/components/table-tree.d.ts +96 -0
- package/dist/components/table-tree.js +137 -0
- package/dist/components/table.d.ts +78 -0
- package/dist/components/table.js +119 -0
- package/dist/components/tabs.d.ts +27 -0
- package/dist/components/tabs.js +79 -0
- package/dist/components/textarea.d.ts +3 -0
- package/dist/components/textarea.js +20 -0
- package/dist/components/theme.d.ts +41 -0
- package/dist/components/theme.js +60 -0
- package/dist/components/thinking.d.ts +68 -0
- package/dist/components/thinking.js +94 -0
- package/dist/components/toast.d.ts +39 -0
- package/dist/components/toast.js +110 -0
- package/dist/components/toolbar.d.ts +25 -0
- package/dist/components/toolbar.js +92 -0
- package/dist/components/tooltip.d.ts +23 -0
- package/dist/components/tooltip.js +27 -0
- package/dist/components/top-nav.d.ts +129 -0
- package/dist/components/top-nav.js +211 -0
- package/dist/components/truncate.d.ts +29 -0
- package/dist/components/truncate.js +138 -0
- package/dist/deprecated.d.ts +43 -0
- package/dist/deprecated.js +47 -0
- package/dist/index.d.ts +93 -0
- package/dist/index.js +93 -0
- package/dist/lib/cn.d.ts +5 -0
- package/dist/lib/cn.js +14 -0
- package/dist/lib/color.d.ts +136 -0
- package/dist/lib/color.js +307 -0
- package/dist/lib/contrast.d.ts +53 -0
- package/dist/lib/contrast.js +169 -0
- package/dist/lib/create-theme.d.ts +240 -0
- package/dist/lib/create-theme.js +461 -0
- package/dist/lib/harmony.d.ts +102 -0
- package/dist/lib/harmony.js +426 -0
- package/dist/lib/import-theme.d.ts +22 -0
- package/dist/lib/import-theme.js +221 -0
- package/dist/lib/overflow-scroll.d.ts +25 -0
- package/dist/lib/overflow-scroll.js +66 -0
- package/dist/lib/radio-toggle-render.d.ts +3 -0
- package/dist/lib/radio-toggle-render.js +9 -0
- package/dist/lib/reorder.d.ts +38 -0
- package/dist/lib/reorder.js +193 -0
- package/dist/lib/spacing.d.ts +101 -0
- package/dist/lib/spacing.js +118 -0
- package/dist/lib/tailwind-palette.d.ts +5 -0
- package/dist/lib/tailwind-palette.js +351 -0
- package/dist/lib/truncate.d.ts +18 -0
- package/dist/lib/truncate.js +69 -0
- package/dist/lib/use-single-value.d.ts +5 -0
- package/dist/lib/use-single-value.js +17 -0
- package/dist/styles.css +7988 -0
- package/mcp/server.mjs +87590 -0
- package/package.json +58 -5
- package/skills/continuum-fix-padding/SKILL.md +69 -0
- package/skills/continuum-fix-padding/scripts/compute.ts +155 -0
- package/skills/continuum-fix-padding/scripts/spacing.ts +189 -0
- package/skills/continuum-taste/SKILL.md +186 -0
- package/skills/continuum-taste/assets/tokens.css +647 -0
- package/skills/continuum-taste/references/charts.md +375 -0
- package/skills/continuum-taste/references/components.md +1369 -0
- package/skills/continuum-taste/references/foundations.md +539 -0
- package/skills/continuum-taste/references/media/canvas.md +75 -0
- package/skills/continuum-taste/references/media/css.md +61 -0
- package/skills/continuum-taste/references/media/react.md +161 -0
- package/skills/continuum-taste/references/method.md +87 -0
- package/skills/continuum-taste/references/pages.md +391 -0
- package/skills/continuum-taste/references/patterns.md +77 -0
- package/skills/continuum-taste/references/screens/agent.tsx +450 -0
- package/skills/continuum-taste/references/screens/analytics-overview.tsx +377 -0
- package/skills/continuum-taste/references/screens/checkout-funnel.tsx +193 -0
- package/skills/continuum-taste/references/screens/customer.tsx +475 -0
- package/skills/continuum-taste/references/screens/customers.tsx +608 -0
- package/skills/continuum-taste/references/screens/index.tsx +72 -0
- package/skills/continuum-taste/references/screens/invoice-editor.tsx +558 -0
- package/skills/continuum-taste/references/screens/new-customer.tsx +205 -0
- package/skills/continuum-taste/references/screens/overview.tsx +196 -0
- package/skills/continuum-taste/references/screens/report-generator.tsx +329 -0
- package/skills/continuum-taste/references/screens/risk-report.tsx +577 -0
- package/skills/continuum-taste/references/screens/sequence-editor.tsx +455 -0
- package/skills/continuum-taste/references/screens/settings.tsx +288 -0
- package/skills/continuum-taste/references/screens/shared.tsx +204 -0
- package/skills/continuum-taste/references/screens/sign-in.tsx +104 -0
- package/skills/continuum-taste/references/screens/workspace.tsx +596 -0
- package/skills/continuum-taste/references/theming.md +95 -0
- package/src/charts/area-chart.tsx +17 -0
- package/src/charts/bar-chart.tsx +15 -0
- package/src/charts/bubble-chart.tsx +913 -0
- package/src/charts/bullet-chart.tsx +419 -0
- package/src/charts/candlestick-chart.tsx +1242 -0
- package/src/charts/cartesian.tsx +1437 -0
- package/src/charts/chart-card.tsx +394 -0
- package/src/charts/donut-chart.tsx +299 -0
- package/src/charts/funnel-chart.tsx +373 -0
- package/src/charts/horizontal-bar-chart.tsx +991 -0
- package/src/charts/index.ts +76 -0
- package/src/charts/legend.tsx +59 -0
- package/src/charts/lib/candlestick-window.ts +42 -0
- package/src/charts/lib/color.ts +110 -0
- package/src/charts/lib/format.ts +42 -0
- package/src/charts/lib/frame.ts +6 -0
- package/src/charts/lib/layout.ts +78 -0
- package/src/charts/lib/path.ts +225 -0
- package/src/charts/lib/scale.ts +91 -0
- package/src/charts/lib/use-active.ts +87 -0
- package/src/charts/lib/use-size.ts +45 -0
- package/src/charts/line-chart.tsx +16 -0
- package/src/charts/radar-chart.tsx +485 -0
- package/src/charts/rose-chart.tsx +553 -0
- package/src/charts/sparkline.tsx +293 -0
- package/src/charts/tooltip.tsx +105 -0
- package/src/charts/treemap.tsx +271 -0
- package/src/charts/uptime-strip.tsx +469 -0
- package/src/components/accordion.tsx +136 -0
- package/src/components/activity-item.tsx +43 -0
- package/src/components/alert.tsx +135 -0
- package/src/components/assistant-answer.tsx +185 -0
- package/src/components/assistant-prompt.tsx +352 -0
- package/src/components/avatar.tsx +157 -0
- package/src/components/badge.tsx +55 -0
- package/src/components/bar-list.tsx +350 -0
- package/src/components/block-slider.tsx +189 -0
- package/src/components/branch-graph.tsx +580 -0
- package/src/components/breadcrumb.tsx +173 -0
- package/src/components/button.tsx +157 -0
- package/src/components/calendar.tsx +154 -0
- package/src/components/card.tsx +296 -0
- package/src/components/chat-attachment.tsx +255 -0
- package/src/components/chat-event.tsx +34 -0
- package/src/components/chat-message.tsx +332 -0
- package/src/components/chat-reference.tsx +100 -0
- package/src/components/chat-shared.ts +41 -0
- package/src/components/chat-suggestions.tsx +188 -0
- package/src/components/chat-thread.tsx +348 -0
- package/src/components/chat-typing.tsx +155 -0
- package/src/components/checkbox.tsx +83 -0
- package/src/components/checklist.tsx +146 -0
- package/src/components/chip.tsx +152 -0
- package/src/components/code-input.tsx +406 -0
- package/src/components/color-picker.tsx +1385 -0
- package/src/components/combobox.tsx +420 -0
- package/src/components/command-menu.tsx +405 -0
- package/src/components/copy-button.tsx +87 -0
- package/src/components/data-grid.tsx +965 -0
- package/src/components/date-picker.tsx +652 -0
- package/src/components/description-list.tsx +39 -0
- package/src/components/dialog.tsx +367 -0
- package/src/components/dock.tsx +192 -0
- package/src/components/drop-zone.tsx +283 -0
- package/src/components/dropdown-menu.tsx +323 -0
- package/src/components/editable-cell.tsx +219 -0
- package/src/components/empty-state.tsx +50 -0
- package/src/components/field.tsx +148 -0
- package/src/components/filter-condition.tsx +463 -0
- package/src/components/filter-menu.tsx +196 -0
- package/src/components/header-bar.tsx +220 -0
- package/src/components/hold-button.tsx +146 -0
- package/src/components/icon-tile.tsx +43 -0
- package/src/components/info-tip.tsx +87 -0
- package/src/components/input.tsx +136 -0
- package/src/components/kbd.tsx +17 -0
- package/src/components/menu-item.tsx +216 -0
- package/src/components/message-composer.tsx +458 -0
- package/src/components/nav-rail.tsx +235 -0
- package/src/components/notice.tsx +133 -0
- package/src/components/optimistic-undo.ts +68 -0
- package/src/components/option-row.tsx +54 -0
- package/src/components/page-content.tsx +44 -0
- package/src/components/pagination.tsx +182 -0
- package/src/components/popover.tsx +113 -0
- package/src/components/progress.tsx +222 -0
- package/src/components/radio-group.tsx +80 -0
- package/src/components/readonly-field.tsx +251 -0
- package/src/components/segmented-control.tsx +205 -0
- package/src/components/select.tsx +259 -0
- package/src/components/separator.tsx +30 -0
- package/src/components/setting-row.tsx +277 -0
- package/src/components/sidebar-switcher.tsx +134 -0
- package/src/components/sidebar.tsx +804 -0
- package/src/components/skeleton.tsx +57 -0
- package/src/components/slider.tsx +106 -0
- package/src/components/spinner.tsx +43 -0
- package/src/components/status-dot.tsx +100 -0
- package/src/components/stepper.tsx +459 -0
- package/src/components/switch.tsx +54 -0
- package/src/components/table-cell.tsx +239 -0
- package/src/components/table-context.ts +135 -0
- package/src/components/table-head.tsx +393 -0
- package/src/components/table-tree.tsx +357 -0
- package/src/components/table.tsx +295 -0
- package/src/components/tabs.tsx +191 -0
- package/src/components/textarea.tsx +44 -0
- package/src/components/theme.tsx +120 -0
- package/src/components/thinking.tsx +295 -0
- package/src/components/toast.tsx +308 -0
- package/src/components/toolbar.tsx +166 -0
- package/src/components/tooltip.tsx +84 -0
- package/src/components/top-nav.tsx +660 -0
- package/src/components/truncate.tsx +234 -0
- package/src/deprecated.ts +61 -0
- package/src/index.ts +184 -0
- package/src/lib/cn.ts +22 -0
- package/src/lib/color.ts +469 -0
- package/src/lib/contrast.ts +220 -0
- package/src/lib/create-theme.ts +659 -0
- package/src/lib/harmony.ts +609 -0
- package/src/lib/import-theme.ts +272 -0
- package/src/lib/overflow-scroll.tsx +108 -0
- package/src/lib/radio-toggle-render.tsx +15 -0
- package/src/lib/reorder.ts +222 -0
- package/src/lib/spacing.ts +189 -0
- package/src/lib/tailwind-palette.ts +355 -0
- package/src/lib/truncate.ts +79 -0
- package/src/lib/use-single-value.ts +23 -0
- package/src/styles/continuum.css +3 -0
- package/src/styles/theme.css +524 -0
- package/src/styles/tokens.css +647 -0
|
@@ -0,0 +1,463 @@
|
|
|
1
|
+
'use client';
|
|
2
|
+
|
|
3
|
+
import type { Menu as MenuPrimitive } from '@base-ui/react/menu';
|
|
4
|
+
import { Toolbar as ToolbarPrimitive } from '@base-ui/react/toolbar';
|
|
5
|
+
import { ListFilter, X } from 'lucide-react';
|
|
6
|
+
import { type ComponentProps, type ReactNode, useState } from 'react';
|
|
7
|
+
import { cn, type WithClassName } from '../lib/cn';
|
|
8
|
+
import { Button, type ButtonProps, focusRing, touchTarget } from './button';
|
|
9
|
+
import {
|
|
10
|
+
DropdownMenu,
|
|
11
|
+
DropdownMenuCheckboxItem,
|
|
12
|
+
DropdownMenuContent,
|
|
13
|
+
type DropdownMenuContentProps,
|
|
14
|
+
DropdownMenuEmpty,
|
|
15
|
+
DropdownMenuItem,
|
|
16
|
+
DropdownMenuRadioGroup,
|
|
17
|
+
DropdownMenuRadioItem,
|
|
18
|
+
DropdownMenuSearch,
|
|
19
|
+
DropdownMenuTrigger,
|
|
20
|
+
} from './dropdown-menu';
|
|
21
|
+
import { Toolbar, ToolbarButton, type ToolbarProps } from './toolbar';
|
|
22
|
+
|
|
23
|
+
/*
|
|
24
|
+
* Conditional filters: each applied filter is a token that reads as a sentence, field · operator ·
|
|
25
|
+
* value, and every part of the sentence is the control that changes it. The token is the inline
|
|
26
|
+
* toolbar (flush segments split by hairlines on the control row), so the applied filters are the
|
|
27
|
+
* controls and need no second display. They sit in a table's own bar, beside the search and the
|
|
28
|
+
* selects a table keeps permanently visible, for a table whose filtering is the work.
|
|
29
|
+
*/
|
|
30
|
+
|
|
31
|
+
export type FilterOperator = { value: string; label: string };
|
|
32
|
+
|
|
33
|
+
/** The operators each kind of field offers; the first is the default when a condition is added. */
|
|
34
|
+
export const filterOperators = {
|
|
35
|
+
many: [
|
|
36
|
+
{ value: 'any', label: 'has any of' },
|
|
37
|
+
{ value: 'all', label: 'has all of' },
|
|
38
|
+
{ value: 'none', label: 'has none of' },
|
|
39
|
+
{ value: 'empty', label: 'is empty' },
|
|
40
|
+
{ value: 'notEmpty', label: 'is not empty' },
|
|
41
|
+
],
|
|
42
|
+
one: [
|
|
43
|
+
{ value: 'is', label: 'is' },
|
|
44
|
+
{ value: 'isNot', label: 'is not' },
|
|
45
|
+
],
|
|
46
|
+
boolean: [
|
|
47
|
+
{ value: 'on', label: 'is on' },
|
|
48
|
+
{ value: 'off', label: 'is off' },
|
|
49
|
+
],
|
|
50
|
+
number: [
|
|
51
|
+
{ value: 'eq', label: 'is' },
|
|
52
|
+
{ value: 'ne', label: 'is not' },
|
|
53
|
+
{ value: 'gt', label: 'is more than' },
|
|
54
|
+
{ value: 'lt', label: 'is less than' },
|
|
55
|
+
],
|
|
56
|
+
date: [
|
|
57
|
+
{ value: 'is', label: 'is' },
|
|
58
|
+
{ value: 'before', label: 'is before' },
|
|
59
|
+
{ value: 'after', label: 'is after' },
|
|
60
|
+
{ value: 'last', label: 'is in the last' },
|
|
61
|
+
],
|
|
62
|
+
text: [
|
|
63
|
+
{ value: 'contains', label: 'contains' },
|
|
64
|
+
{ value: 'notContains', label: 'does not contain' },
|
|
65
|
+
{ value: 'is', label: 'is' },
|
|
66
|
+
{ value: 'starts', label: 'starts with' },
|
|
67
|
+
],
|
|
68
|
+
} as const satisfies Record<string, readonly FilterOperator[]>;
|
|
69
|
+
|
|
70
|
+
export type FilterKind = keyof typeof filterOperators;
|
|
71
|
+
|
|
72
|
+
/** The operator a condition starts with when it is added: the first of its kind. */
|
|
73
|
+
export const defaultFilterOperator = (kind: FilterKind): string => filterOperators[kind][0].value;
|
|
74
|
+
|
|
75
|
+
/** Operators that are a whole sentence on their own ("is empty", "is on"): they take no value. */
|
|
76
|
+
const valuelessOperators: ReadonlySet<string> = new Set(['empty', 'notEmpty', 'on', 'off']);
|
|
77
|
+
|
|
78
|
+
/** Whether an operator needs a value after it; the value segment is left out when it does not. */
|
|
79
|
+
export const filterOperatorTakesValue = (operator: string) => !valuelessOperators.has(operator);
|
|
80
|
+
|
|
81
|
+
/** Every typed word appears in the label, so "de vr" finds "Anna de Vries". */
|
|
82
|
+
const matchesQuery = (label: ReactNode, query: string) => {
|
|
83
|
+
const text = String(label ?? '').toLowerCase();
|
|
84
|
+
return query
|
|
85
|
+
.toLowerCase()
|
|
86
|
+
.split(/\s+/)
|
|
87
|
+
.filter(Boolean)
|
|
88
|
+
.every(word => text.includes(word));
|
|
89
|
+
};
|
|
90
|
+
|
|
91
|
+
export type FilterConditionsProps = ComponentProps<'div'> & {
|
|
92
|
+
/** Shows a ghost "Clear all" after the conditions. */
|
|
93
|
+
onClear?: () => void;
|
|
94
|
+
clearLabel?: ReactNode;
|
|
95
|
+
/** The add button, last in the row, after Clear all. */
|
|
96
|
+
add?: ReactNode;
|
|
97
|
+
};
|
|
98
|
+
|
|
99
|
+
/** The applied conditions in a row, then Clear all once there is anything, then the add button. */
|
|
100
|
+
export const FilterConditions = ({
|
|
101
|
+
onClear,
|
|
102
|
+
clearLabel = 'Clear all',
|
|
103
|
+
add,
|
|
104
|
+
className,
|
|
105
|
+
children,
|
|
106
|
+
...props
|
|
107
|
+
}: FilterConditionsProps) => (
|
|
108
|
+
<div
|
|
109
|
+
className={cn('flex min-w-0 flex-wrap items-center gap-2', className)}
|
|
110
|
+
data-slot="filter-conditions"
|
|
111
|
+
{...props}
|
|
112
|
+
>
|
|
113
|
+
{children}
|
|
114
|
+
{onClear && (
|
|
115
|
+
<Button data-slot="filter-conditions-clear" onClick={onClear} variant="ghost">
|
|
116
|
+
{clearLabel}
|
|
117
|
+
</Button>
|
|
118
|
+
)}
|
|
119
|
+
{add}
|
|
120
|
+
</div>
|
|
121
|
+
);
|
|
122
|
+
|
|
123
|
+
export type FilterConditionProps = WithClassName<ToolbarProps>;
|
|
124
|
+
|
|
125
|
+
/**
|
|
126
|
+
* One condition: the inline toolbar, without the float shadow, since it sits in a bar and does not
|
|
127
|
+
* float. Conditions wrap as whole tokens; one wider than its row on its own shrinks to the row, and
|
|
128
|
+
* its value truncates rather than the token clipping.
|
|
129
|
+
*/
|
|
130
|
+
export const FilterCondition = ({ className, ...props }: FilterConditionProps) => (
|
|
131
|
+
<Toolbar
|
|
132
|
+
className={cn('min-w-0 shadow-none', className)}
|
|
133
|
+
data-slot="filter-condition"
|
|
134
|
+
variant="inline"
|
|
135
|
+
{...props}
|
|
136
|
+
/>
|
|
137
|
+
);
|
|
138
|
+
|
|
139
|
+
/** The field the condition is on: its icon and name, the one part that is not a control. */
|
|
140
|
+
export const FilterConditionField = ({
|
|
141
|
+
icon,
|
|
142
|
+
className,
|
|
143
|
+
children,
|
|
144
|
+
...props
|
|
145
|
+
}: ComponentProps<'span'> & { icon?: ReactNode }) => (
|
|
146
|
+
<span
|
|
147
|
+
className={cn(
|
|
148
|
+
'inline-flex items-center gap-2 whitespace-nowrap border-neutral-200 border-r px-2.5 font-medium text-neutral-950 text-xs [&_svg:not([class*=size-])]:size-icon [&_svg]:shrink-0 [&_svg]:text-neutral-600',
|
|
149
|
+
className,
|
|
150
|
+
)}
|
|
151
|
+
data-slot="filter-condition-field"
|
|
152
|
+
{...props}
|
|
153
|
+
>
|
|
154
|
+
{icon}
|
|
155
|
+
{children}
|
|
156
|
+
</span>
|
|
157
|
+
);
|
|
158
|
+
|
|
159
|
+
export type FilterConditionSegmentProps = ComponentProps<typeof ToolbarButton> & {
|
|
160
|
+
/** The operator reads quieter than the field and the value, so the sentence has one stress. */
|
|
161
|
+
muted?: boolean;
|
|
162
|
+
};
|
|
163
|
+
|
|
164
|
+
/** A segment of the sentence that opens its own menu: the operator, or the value. */
|
|
165
|
+
export const FilterConditionSegment = ({
|
|
166
|
+
muted,
|
|
167
|
+
className,
|
|
168
|
+
...props
|
|
169
|
+
}: FilterConditionSegmentProps) => (
|
|
170
|
+
<ToolbarButton
|
|
171
|
+
className={cn(
|
|
172
|
+
'px-2.5',
|
|
173
|
+
muted ? 'font-normal text-neutral-600' : 'text-neutral-950',
|
|
174
|
+
className,
|
|
175
|
+
)}
|
|
176
|
+
data-slot="filter-condition-segment"
|
|
177
|
+
{...props}
|
|
178
|
+
/>
|
|
179
|
+
);
|
|
180
|
+
|
|
181
|
+
export type FilterConditionOperatorProps = {
|
|
182
|
+
options: readonly FilterOperator[];
|
|
183
|
+
value: string;
|
|
184
|
+
onValueChange: (value: string) => void;
|
|
185
|
+
/** Where the menu portals, for a preview that must keep it inside a box. */
|
|
186
|
+
container?: DropdownMenuContentProps['container'];
|
|
187
|
+
};
|
|
188
|
+
|
|
189
|
+
/** The operator segment: a radio menu of what the field can do. */
|
|
190
|
+
export const FilterConditionOperator = ({
|
|
191
|
+
options,
|
|
192
|
+
value,
|
|
193
|
+
onValueChange,
|
|
194
|
+
container,
|
|
195
|
+
}: FilterConditionOperatorProps) => (
|
|
196
|
+
<DropdownMenu>
|
|
197
|
+
<DropdownMenuTrigger render={<FilterConditionSegment muted />}>
|
|
198
|
+
{options.find(option => option.value === value)?.label ?? value}
|
|
199
|
+
</DropdownMenuTrigger>
|
|
200
|
+
<DropdownMenuContent
|
|
201
|
+
align="start"
|
|
202
|
+
container={container}
|
|
203
|
+
data-slot="filter-condition-operator-menu"
|
|
204
|
+
>
|
|
205
|
+
<DropdownMenuRadioGroup
|
|
206
|
+
onValueChange={next => onValueChange(String(next))}
|
|
207
|
+
value={value}
|
|
208
|
+
>
|
|
209
|
+
{options.map(option => (
|
|
210
|
+
<DropdownMenuRadioItem key={option.value} value={option.value}>
|
|
211
|
+
{option.label}
|
|
212
|
+
</DropdownMenuRadioItem>
|
|
213
|
+
))}
|
|
214
|
+
</DropdownMenuRadioGroup>
|
|
215
|
+
</DropdownMenuContent>
|
|
216
|
+
</DropdownMenu>
|
|
217
|
+
);
|
|
218
|
+
|
|
219
|
+
export type FilterConditionValueProps = {
|
|
220
|
+
options: readonly { value: string; label?: ReactNode }[];
|
|
221
|
+
/** The picked values; one for a single-valued field. */
|
|
222
|
+
value: readonly string[];
|
|
223
|
+
onValueChange: (value: string[]) => void;
|
|
224
|
+
/** Several values may be picked (checkboxes); otherwise one (radios). */
|
|
225
|
+
multiple?: boolean;
|
|
226
|
+
/** Shown while nothing is picked. */
|
|
227
|
+
placeholder?: ReactNode;
|
|
228
|
+
/** A search on top of the menu, for a long list of values. */
|
|
229
|
+
searchable?: boolean;
|
|
230
|
+
/** The search's placeholder; "Search values" by default. */
|
|
231
|
+
searchPlaceholder?: string;
|
|
232
|
+
/** Shown when the search matches nothing; "No values match" by default. */
|
|
233
|
+
emptyLabel?: ReactNode;
|
|
234
|
+
/**
|
|
235
|
+
* The condition's operator. Given one that takes no value ("is empty", "is on"), the segment
|
|
236
|
+
* is left out, so the sentence ends at the operator.
|
|
237
|
+
*/
|
|
238
|
+
operator?: string;
|
|
239
|
+
container?: DropdownMenuContentProps['container'];
|
|
240
|
+
};
|
|
241
|
+
|
|
242
|
+
/** "Maya Chen +1": the first picked value and how many more, so the token keeps its width. */
|
|
243
|
+
export const filterValueSummary = (
|
|
244
|
+
picked: readonly ReactNode[],
|
|
245
|
+
placeholder: ReactNode = 'Select',
|
|
246
|
+
) =>
|
|
247
|
+
picked.length === 0 ? (
|
|
248
|
+
<span className="min-w-0 truncate">{placeholder}</span>
|
|
249
|
+
) : (
|
|
250
|
+
<>
|
|
251
|
+
{/* The first value is what gives way when the token runs out of room; the count stays. */}
|
|
252
|
+
<span className="min-w-0 truncate">{picked[0]}</span>
|
|
253
|
+
{picked.length > 1 && (
|
|
254
|
+
<span className="font-normal text-neutral-600">+{picked.length - 1}</span>
|
|
255
|
+
)}
|
|
256
|
+
</>
|
|
257
|
+
);
|
|
258
|
+
|
|
259
|
+
/** The value segment: the field's values to tick (several) or pick (one). */
|
|
260
|
+
export const FilterConditionValue = ({
|
|
261
|
+
options,
|
|
262
|
+
value,
|
|
263
|
+
onValueChange,
|
|
264
|
+
multiple = false,
|
|
265
|
+
placeholder,
|
|
266
|
+
searchable = false,
|
|
267
|
+
searchPlaceholder = 'Search values',
|
|
268
|
+
emptyLabel = 'No values match',
|
|
269
|
+
operator,
|
|
270
|
+
container,
|
|
271
|
+
}: FilterConditionValueProps) => {
|
|
272
|
+
const [query, setQuery] = useState('');
|
|
273
|
+
if (operator !== undefined && !filterOperatorTakesValue(operator)) return null;
|
|
274
|
+
const picked = value.map(item => options.find(option => option.value === item)?.label ?? item);
|
|
275
|
+
const shown = searchable
|
|
276
|
+
? options.filter(option => matchesQuery(option.label ?? option.value, query))
|
|
277
|
+
: options;
|
|
278
|
+
|
|
279
|
+
return (
|
|
280
|
+
<DropdownMenu onOpenChange={open => !open && setQuery('')}>
|
|
281
|
+
{/* The one segment that may narrow, so a long value truncates before the token clips. */}
|
|
282
|
+
<DropdownMenuTrigger render={<FilterConditionSegment className="min-w-0" />}>
|
|
283
|
+
<span className="inline-flex min-w-0 items-center gap-1.5">
|
|
284
|
+
{filterValueSummary(picked, placeholder)}
|
|
285
|
+
</span>
|
|
286
|
+
</DropdownMenuTrigger>
|
|
287
|
+
<DropdownMenuContent
|
|
288
|
+
align="start"
|
|
289
|
+
container={container}
|
|
290
|
+
data-slot="filter-condition-value-menu"
|
|
291
|
+
>
|
|
292
|
+
{searchable && (
|
|
293
|
+
<DropdownMenuSearch
|
|
294
|
+
onValueChange={setQuery}
|
|
295
|
+
placeholder={searchPlaceholder}
|
|
296
|
+
value={query}
|
|
297
|
+
/>
|
|
298
|
+
)}
|
|
299
|
+
{shown.length === 0 && <DropdownMenuEmpty>{emptyLabel}</DropdownMenuEmpty>}
|
|
300
|
+
{multiple ? (
|
|
301
|
+
shown.map(option => (
|
|
302
|
+
<DropdownMenuCheckboxItem
|
|
303
|
+
checked={value.includes(option.value)}
|
|
304
|
+
closeOnClick={false}
|
|
305
|
+
key={option.value}
|
|
306
|
+
onCheckedChange={on =>
|
|
307
|
+
onValueChange(
|
|
308
|
+
on
|
|
309
|
+
? [...value, option.value]
|
|
310
|
+
: value.filter(item => item !== option.value),
|
|
311
|
+
)
|
|
312
|
+
}
|
|
313
|
+
>
|
|
314
|
+
{option.label ?? option.value}
|
|
315
|
+
</DropdownMenuCheckboxItem>
|
|
316
|
+
))
|
|
317
|
+
) : (
|
|
318
|
+
<DropdownMenuRadioGroup
|
|
319
|
+
onValueChange={next => onValueChange([String(next)])}
|
|
320
|
+
value={value[0] ?? ''}
|
|
321
|
+
>
|
|
322
|
+
{shown.map(option => (
|
|
323
|
+
<DropdownMenuRadioItem key={option.value} value={option.value}>
|
|
324
|
+
{option.label ?? option.value}
|
|
325
|
+
</DropdownMenuRadioItem>
|
|
326
|
+
))}
|
|
327
|
+
</DropdownMenuRadioGroup>
|
|
328
|
+
)}
|
|
329
|
+
</DropdownMenuContent>
|
|
330
|
+
</DropdownMenu>
|
|
331
|
+
);
|
|
332
|
+
};
|
|
333
|
+
|
|
334
|
+
export type FilterConditionInputProps = Omit<
|
|
335
|
+
ComponentProps<typeof ToolbarPrimitive.Input>,
|
|
336
|
+
'className'
|
|
337
|
+
> & {
|
|
338
|
+
className?: string;
|
|
339
|
+
/**
|
|
340
|
+
* The condition's operator. Given one that takes no value ("is empty"), the segment is left
|
|
341
|
+
* out, so the sentence ends at the operator.
|
|
342
|
+
*/
|
|
343
|
+
operator?: string;
|
|
344
|
+
};
|
|
345
|
+
|
|
346
|
+
/**
|
|
347
|
+
* The value segment typed rather than picked, for a number, a date or a piece of text ("is more
|
|
348
|
+
* than 500", "contains invoice"). It reads as the value does, 13/500 `neutral-950`, and grows with
|
|
349
|
+
* what is typed.
|
|
350
|
+
*/
|
|
351
|
+
export const FilterConditionInput = ({
|
|
352
|
+
operator,
|
|
353
|
+
className,
|
|
354
|
+
...props
|
|
355
|
+
}: FilterConditionInputProps) => {
|
|
356
|
+
if (operator !== undefined && !filterOperatorTakesValue(operator)) return null;
|
|
357
|
+
|
|
358
|
+
return (
|
|
359
|
+
<ToolbarPrimitive.Input
|
|
360
|
+
className={cn(
|
|
361
|
+
'field-sizing-content min-w-16 max-w-48 border-neutral-200 not-last:border-r bg-transparent px-2.5 font-medium text-neutral-950 text-xs outline-none placeholder:text-neutral-600',
|
|
362
|
+
// 16px on touch, so iOS does not zoom the page in when the field takes focus.
|
|
363
|
+
'pointer-coarse:text-base',
|
|
364
|
+
focusRing,
|
|
365
|
+
// The bar clips overflow, so the ring sits inside the segment, as a button's does.
|
|
366
|
+
'focus-visible:-outline-offset-2',
|
|
367
|
+
className,
|
|
368
|
+
)}
|
|
369
|
+
data-slot="filter-condition-input"
|
|
370
|
+
{...props}
|
|
371
|
+
/>
|
|
372
|
+
);
|
|
373
|
+
};
|
|
374
|
+
|
|
375
|
+
/** The × that ends the sentence and removes the condition. */
|
|
376
|
+
export const FilterConditionRemove = ({
|
|
377
|
+
className,
|
|
378
|
+
'aria-label': ariaLabel = 'Remove filter',
|
|
379
|
+
...props
|
|
380
|
+
}: ComponentProps<typeof ToolbarButton>) => (
|
|
381
|
+
<ToolbarButton
|
|
382
|
+
aria-label={ariaLabel}
|
|
383
|
+
// A finger's 44px round the small ×, on a touch screen only; the bar's edge still clips it.
|
|
384
|
+
className={cn('px-2 text-neutral-500', touchTarget, className)}
|
|
385
|
+
data-slot="filter-condition-remove"
|
|
386
|
+
{...props}
|
|
387
|
+
>
|
|
388
|
+
<X className="size-3.5" strokeWidth={2} />
|
|
389
|
+
</ToolbarButton>
|
|
390
|
+
);
|
|
391
|
+
|
|
392
|
+
export type FilterField = { id: string; label: ReactNode; icon?: ReactNode };
|
|
393
|
+
|
|
394
|
+
export type FilterConditionAddProps = Omit<ButtonProps, 'onSelect'> & {
|
|
395
|
+
/** The fields a condition can be added on; one already applied is left out by the caller. */
|
|
396
|
+
fields: readonly FilterField[];
|
|
397
|
+
onAdd: (id: string) => void;
|
|
398
|
+
/** A search on top of the field list, for a long one. */
|
|
399
|
+
searchable?: boolean;
|
|
400
|
+
/** The search's placeholder; "Search fields" by default. */
|
|
401
|
+
searchPlaceholder?: string;
|
|
402
|
+
/** Shown when the search matches nothing; "No fields match" by default. */
|
|
403
|
+
emptyLabel?: ReactNode;
|
|
404
|
+
container?: DropdownMenuContentProps['container'];
|
|
405
|
+
menuProps?: Partial<MenuPrimitive.Root.Props>;
|
|
406
|
+
};
|
|
407
|
+
|
|
408
|
+
/** The button that adds a condition: a 34 secondary square with the filter icon, opening the fields. */
|
|
409
|
+
export const FilterConditionAdd = ({
|
|
410
|
+
fields,
|
|
411
|
+
onAdd,
|
|
412
|
+
searchable = false,
|
|
413
|
+
searchPlaceholder = 'Search fields',
|
|
414
|
+
emptyLabel = 'No fields match',
|
|
415
|
+
container,
|
|
416
|
+
menuProps,
|
|
417
|
+
'aria-label': ariaLabel = 'Add filter',
|
|
418
|
+
size = 'icon',
|
|
419
|
+
variant = 'secondary',
|
|
420
|
+
...props
|
|
421
|
+
}: FilterConditionAddProps) => {
|
|
422
|
+
const [query, setQuery] = useState('');
|
|
423
|
+
const shown = searchable ? fields.filter(field => matchesQuery(field.label, query)) : fields;
|
|
424
|
+
|
|
425
|
+
return (
|
|
426
|
+
<DropdownMenu onOpenChange={open => !open && setQuery('')} {...menuProps}>
|
|
427
|
+
<DropdownMenuTrigger
|
|
428
|
+
data-slot="filter-condition-add"
|
|
429
|
+
render={
|
|
430
|
+
<Button
|
|
431
|
+
aria-label={ariaLabel}
|
|
432
|
+
disabled={fields.length === 0}
|
|
433
|
+
size={size}
|
|
434
|
+
variant={variant}
|
|
435
|
+
{...props}
|
|
436
|
+
/>
|
|
437
|
+
}
|
|
438
|
+
>
|
|
439
|
+
<ListFilter strokeWidth={2} />
|
|
440
|
+
</DropdownMenuTrigger>
|
|
441
|
+
<DropdownMenuContent
|
|
442
|
+
align="start"
|
|
443
|
+
container={container}
|
|
444
|
+
data-slot="filter-condition-add-menu"
|
|
445
|
+
>
|
|
446
|
+
{searchable && (
|
|
447
|
+
<DropdownMenuSearch
|
|
448
|
+
onValueChange={setQuery}
|
|
449
|
+
placeholder={searchPlaceholder}
|
|
450
|
+
value={query}
|
|
451
|
+
/>
|
|
452
|
+
)}
|
|
453
|
+
{shown.length === 0 && <DropdownMenuEmpty>{emptyLabel}</DropdownMenuEmpty>}
|
|
454
|
+
{shown.map(field => (
|
|
455
|
+
<DropdownMenuItem key={field.id} onClick={() => onAdd(field.id)}>
|
|
456
|
+
{field.icon}
|
|
457
|
+
{field.label}
|
|
458
|
+
</DropdownMenuItem>
|
|
459
|
+
))}
|
|
460
|
+
</DropdownMenuContent>
|
|
461
|
+
</DropdownMenu>
|
|
462
|
+
);
|
|
463
|
+
};
|
|
@@ -0,0 +1,196 @@
|
|
|
1
|
+
'use client';
|
|
2
|
+
|
|
3
|
+
import { Menu as MenuPrimitive } from '@base-ui/react/menu';
|
|
4
|
+
import { ListFilter } from 'lucide-react';
|
|
5
|
+
import type { ComponentProps, ReactNode } from 'react';
|
|
6
|
+
import { cn, type WithClassName } from '../lib/cn';
|
|
7
|
+
import { Badge } from './badge';
|
|
8
|
+
import { Button, type ButtonProps } from './button';
|
|
9
|
+
import {
|
|
10
|
+
DropdownMenu,
|
|
11
|
+
DropdownMenuContent,
|
|
12
|
+
type DropdownMenuContentProps,
|
|
13
|
+
DropdownMenuGroup,
|
|
14
|
+
DropdownMenuItem,
|
|
15
|
+
type DropdownMenuItemProps,
|
|
16
|
+
DropdownMenuLabel,
|
|
17
|
+
DropdownMenuSeparator,
|
|
18
|
+
DropdownMenuTrigger,
|
|
19
|
+
} from './dropdown-menu';
|
|
20
|
+
import { MenuItem, MenuItemIndicator } from './menu-item';
|
|
21
|
+
|
|
22
|
+
/** Applied filters as a map of group to picked values, the shape the menu and its chips share. */
|
|
23
|
+
export type FilterValues = Record<string, string[]>;
|
|
24
|
+
|
|
25
|
+
/** Adds or removes one value of a group, dropping the group once it is empty. */
|
|
26
|
+
export const toggleFilter = (values: FilterValues, group: string, value: string): FilterValues => {
|
|
27
|
+
const current = values[group] ?? [];
|
|
28
|
+
const next = current.includes(value)
|
|
29
|
+
? current.filter(item => item !== value)
|
|
30
|
+
: [...current, value];
|
|
31
|
+
const rest = Object.fromEntries(Object.entries(values).filter(([key]) => key !== group));
|
|
32
|
+
return next.length > 0 ? { ...rest, [group]: next } : rest;
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
/** How many values are picked across every group: the count on the trigger. */
|
|
36
|
+
export const countFilters = (values: FilterValues) =>
|
|
37
|
+
Object.values(values).reduce((sum, picked) => sum + picked.length, 0);
|
|
38
|
+
|
|
39
|
+
/** Grouped checkbox filters behind one button; what is picked shows as chips in a `FilterBar`. */
|
|
40
|
+
export const FilterMenu = (props: MenuPrimitive.Root.Props) => <DropdownMenu {...props} />;
|
|
41
|
+
|
|
42
|
+
export type FilterMenuTriggerProps = ButtonProps & {
|
|
43
|
+
/** Picked values; shown as a count beside the label once there are any. */
|
|
44
|
+
count?: number;
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
/** A secondary "Filter" button with the number of filters applied. */
|
|
48
|
+
export const FilterMenuTrigger = ({
|
|
49
|
+
count = 0,
|
|
50
|
+
children = 'Filter',
|
|
51
|
+
variant = 'secondary',
|
|
52
|
+
...props
|
|
53
|
+
}: FilterMenuTriggerProps) => (
|
|
54
|
+
<DropdownMenuTrigger
|
|
55
|
+
data-slot="filter-menu-trigger"
|
|
56
|
+
render={<Button variant={variant} {...props} />}
|
|
57
|
+
>
|
|
58
|
+
<ListFilter strokeWidth={2} />
|
|
59
|
+
{children}
|
|
60
|
+
{count > 0 && (
|
|
61
|
+
// Pulled in so the count sits as close to the edge as the icon does on the other side. On
|
|
62
|
+
// the primary fill it takes the fill's own text colour, as the neutral badge would vanish.
|
|
63
|
+
<Badge
|
|
64
|
+
className={cn(
|
|
65
|
+
'-mr-1.5 ml-0.5 h-5 min-w-5',
|
|
66
|
+
variant === 'primary' && 'bg-on-action/20 text-on-action',
|
|
67
|
+
)}
|
|
68
|
+
variant="count"
|
|
69
|
+
>
|
|
70
|
+
{count}
|
|
71
|
+
</Badge>
|
|
72
|
+
)}
|
|
73
|
+
</DropdownMenuTrigger>
|
|
74
|
+
);
|
|
75
|
+
|
|
76
|
+
/** The menu's panel; a little wider than a row menu, since its labels are values, not verbs. */
|
|
77
|
+
export const FilterMenuContent = ({
|
|
78
|
+
className,
|
|
79
|
+
align = 'start',
|
|
80
|
+
...props
|
|
81
|
+
}: DropdownMenuContentProps) => (
|
|
82
|
+
<DropdownMenuContent
|
|
83
|
+
align={align}
|
|
84
|
+
className={cn('w-filter-menu', className)}
|
|
85
|
+
data-slot="filter-menu-content"
|
|
86
|
+
{...props}
|
|
87
|
+
/>
|
|
88
|
+
);
|
|
89
|
+
|
|
90
|
+
export type FilterMenuGroupProps = WithClassName<MenuPrimitive.Group.Props> & {
|
|
91
|
+
label: ReactNode;
|
|
92
|
+
};
|
|
93
|
+
|
|
94
|
+
/** One dimension to filter by, its label over its options. */
|
|
95
|
+
export const FilterMenuGroup = ({ label, children, ...props }: FilterMenuGroupProps) => (
|
|
96
|
+
<DropdownMenuGroup data-slot="filter-menu-group" {...props}>
|
|
97
|
+
<DropdownMenuLabel>{label}</DropdownMenuLabel>
|
|
98
|
+
{children}
|
|
99
|
+
</DropdownMenuGroup>
|
|
100
|
+
);
|
|
101
|
+
|
|
102
|
+
export type FilterMenuItemProps = WithClassName<MenuPrimitive.CheckboxItem.Props> & {
|
|
103
|
+
/** Leading 16px icon or dot, before the label. */
|
|
104
|
+
icon?: ReactNode;
|
|
105
|
+
/** How many records the value matches, quiet at the end of the row. */
|
|
106
|
+
count?: number;
|
|
107
|
+
};
|
|
108
|
+
|
|
109
|
+
/**
|
|
110
|
+
* One value as a checkbox row. The box is the checkbox drawn small, so the menu reads as a set to
|
|
111
|
+
* tick, not actions to run; the menu stays open while values are ticked.
|
|
112
|
+
*/
|
|
113
|
+
export const FilterMenuItem = ({
|
|
114
|
+
icon,
|
|
115
|
+
count,
|
|
116
|
+
className,
|
|
117
|
+
children,
|
|
118
|
+
closeOnClick = false,
|
|
119
|
+
...props
|
|
120
|
+
}: FilterMenuItemProps) => (
|
|
121
|
+
<MenuItem
|
|
122
|
+
className={className}
|
|
123
|
+
data-slot="filter-menu-item"
|
|
124
|
+
leading={
|
|
125
|
+
<>
|
|
126
|
+
<MenuItemIndicator />
|
|
127
|
+
{icon}
|
|
128
|
+
</>
|
|
129
|
+
}
|
|
130
|
+
render={<MenuPrimitive.CheckboxItem closeOnClick={closeOnClick} {...props} />}
|
|
131
|
+
trailing={
|
|
132
|
+
count !== undefined && <span className="tabular-nums">{count.toLocaleString()}</span>
|
|
133
|
+
}
|
|
134
|
+
>
|
|
135
|
+
{children}
|
|
136
|
+
</MenuItem>
|
|
137
|
+
);
|
|
138
|
+
|
|
139
|
+
export type FilterMenuClearProps = Omit<DropdownMenuItemProps, 'children'> & {
|
|
140
|
+
children?: ReactNode;
|
|
141
|
+
/** Values picked across the groups, as the trigger counts them; at 0 the item is disabled. */
|
|
142
|
+
count?: number;
|
|
143
|
+
};
|
|
144
|
+
|
|
145
|
+
/**
|
|
146
|
+
* "Clear all" at the foot of the menu, under a full-width rule; disabled while nothing is picked.
|
|
147
|
+
* Pass the trigger's `count` and it disables itself; an explicit `disabled` still wins.
|
|
148
|
+
*/
|
|
149
|
+
export const FilterMenuClear = ({
|
|
150
|
+
children = 'Clear all',
|
|
151
|
+
count,
|
|
152
|
+
disabled,
|
|
153
|
+
...props
|
|
154
|
+
}: FilterMenuClearProps) => (
|
|
155
|
+
<>
|
|
156
|
+
<DropdownMenuSeparator />
|
|
157
|
+
<DropdownMenuItem
|
|
158
|
+
data-slot="filter-menu-clear"
|
|
159
|
+
disabled={disabled ?? (count !== undefined && count === 0)}
|
|
160
|
+
{...props}
|
|
161
|
+
>
|
|
162
|
+
{children}
|
|
163
|
+
</DropdownMenuItem>
|
|
164
|
+
</>
|
|
165
|
+
);
|
|
166
|
+
|
|
167
|
+
export type FilterBarProps = ComponentProps<'div'> & {
|
|
168
|
+
/** Shows a ghost "Clear all" after the chips. */
|
|
169
|
+
onClear?: () => void;
|
|
170
|
+
clearLabel?: ReactNode;
|
|
171
|
+
};
|
|
172
|
+
|
|
173
|
+
/**
|
|
174
|
+
* The trigger and the applied filters in one row: put the `FilterMenuTrigger`'s menu first, then a
|
|
175
|
+
* `Chip` per group; "Clear all" follows once there is something to clear.
|
|
176
|
+
*/
|
|
177
|
+
export const FilterBar = ({
|
|
178
|
+
onClear,
|
|
179
|
+
clearLabel = 'Clear all',
|
|
180
|
+
className,
|
|
181
|
+
children,
|
|
182
|
+
...props
|
|
183
|
+
}: FilterBarProps) => (
|
|
184
|
+
<div
|
|
185
|
+
className={cn('flex min-w-0 flex-wrap items-center gap-2', className)}
|
|
186
|
+
data-slot="filter-bar"
|
|
187
|
+
{...props}
|
|
188
|
+
>
|
|
189
|
+
{children}
|
|
190
|
+
{onClear && (
|
|
191
|
+
<Button data-slot="filter-bar-clear" onClick={onClear} variant="ghost">
|
|
192
|
+
{clearLabel}
|
|
193
|
+
</Button>
|
|
194
|
+
)}
|
|
195
|
+
</div>
|
|
196
|
+
);
|