@dashforge/tw 0.3.0-beta → 0.4.0-beta
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/CHANGELOG.md +123 -0
- package/PARITY.md +483 -0
- package/PERFORMANCE.md +169 -0
- package/dist/index.esm.js +493 -2
- package/dist/src/components/Accordion/Accordion.d.ts +21 -0
- package/dist/src/components/Accordion/Accordion.d.ts.map +1 -0
- package/dist/src/components/Accordion/accordion.types.d.ts +74 -0
- package/dist/src/components/Accordion/accordion.types.d.ts.map +1 -0
- package/dist/src/components/Accordion/accordion.variants.d.ts +58 -0
- package/dist/src/components/Accordion/accordion.variants.d.ts.map +1 -0
- package/dist/src/components/Dialog/Dialog.d.ts +17 -0
- package/dist/src/components/Dialog/Dialog.d.ts.map +1 -0
- package/dist/src/components/Dialog/dialog.types.d.ts +75 -0
- package/dist/src/components/Dialog/dialog.types.d.ts.map +1 -0
- package/dist/src/components/Dialog/dialog.variants.d.ts +80 -0
- package/dist/src/components/Dialog/dialog.variants.d.ts.map +1 -0
- package/dist/src/components/Popover/Popover.d.ts +15 -0
- package/dist/src/components/Popover/Popover.d.ts.map +1 -0
- package/dist/src/components/Popover/popover.types.d.ts +45 -0
- package/dist/src/components/Popover/popover.types.d.ts.map +1 -0
- package/dist/src/components/Popover/popover.variants.d.ts +34 -0
- package/dist/src/components/Popover/popover.variants.d.ts.map +1 -0
- package/dist/src/components/Tabs/Tabs.d.ts +15 -0
- package/dist/src/components/Tabs/Tabs.d.ts.map +1 -0
- package/dist/src/components/Tabs/tabs.types.d.ts +55 -0
- package/dist/src/components/Tabs/tabs.types.d.ts.map +1 -0
- package/dist/src/components/Tabs/tabs.variants.d.ts +82 -0
- package/dist/src/components/Tabs/tabs.variants.d.ts.map +1 -0
- package/dist/src/components/Tooltip/Tooltip.d.ts +29 -0
- package/dist/src/components/Tooltip/Tooltip.d.ts.map +1 -0
- package/dist/src/components/Tooltip/tooltip.types.d.ts +47 -0
- package/dist/src/components/Tooltip/tooltip.types.d.ts.map +1 -0
- package/dist/src/components/Tooltip/tooltip.variants.d.ts +34 -0
- package/dist/src/components/Tooltip/tooltip.variants.d.ts.map +1 -0
- package/dist/src/index.d.ts +16 -1
- package/dist/src/index.d.ts.map +1 -1
- package/package.json +8 -3
- package/src/components/Accordion/Accordion.test.tsx +95 -0
- package/src/components/Accordion/Accordion.tsx +97 -0
- package/src/components/Accordion/accordion.types.ts +66 -0
- package/src/components/Accordion/accordion.variants.ts +30 -0
- package/src/components/Dialog/Dialog.test.tsx +96 -0
- package/src/components/Dialog/Dialog.tsx +93 -0
- package/src/components/Dialog/dialog.types.ts +62 -0
- package/src/components/Dialog/dialog.variants.ts +62 -0
- package/src/components/Popover/Popover.test.tsx +72 -0
- package/src/components/Popover/Popover.tsx +55 -0
- package/src/components/Popover/popover.types.ts +42 -0
- package/src/components/Popover/popover.variants.ts +18 -0
- package/src/components/Tabs/Tabs.test.tsx +74 -0
- package/src/components/Tabs/Tabs.tsx +66 -0
- package/src/components/Tabs/tabs.types.ts +49 -0
- package/src/components/Tabs/tabs.variants.ts +58 -0
- package/src/components/Tooltip/Tooltip.test.tsx +73 -0
- package/src/components/Tooltip/Tooltip.tsx +70 -0
- package/src/components/Tooltip/tooltip.types.ts +44 -0
- package/src/components/Tooltip/tooltip.variants.ts +18 -0
- package/src/index.ts +40 -1
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import type { ReactNode } from 'react';
|
|
2
|
+
import type { DialogVariants } from './dialog.variants.js';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Per-slot className overrides for `<Dialog>`. Mirrors the
|
|
6
|
+
* canonical TW slotProps shape: `{ slot?: { className?: string } }`.
|
|
7
|
+
*/
|
|
8
|
+
export interface DialogSlotProps {
|
|
9
|
+
overlay?: { className?: string };
|
|
10
|
+
content?: { className?: string };
|
|
11
|
+
title?: { className?: string };
|
|
12
|
+
description?: { className?: string };
|
|
13
|
+
closeButton?: { className?: string };
|
|
14
|
+
body?: { className?: string };
|
|
15
|
+
actions?: { className?: string };
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Props for the declarative `<Dialog>` component.
|
|
20
|
+
*
|
|
21
|
+
* Built on `@radix-ui/react-dialog`. Use this when:
|
|
22
|
+
* - You want full control over the dialog's open state (controlled
|
|
23
|
+
* from a parent component).
|
|
24
|
+
* - You want to mount a custom-styled dialog body / trigger that
|
|
25
|
+
* composes with the rest of your component tree.
|
|
26
|
+
* - You don't need an imperative `confirm()`-style return value
|
|
27
|
+
* (use `useDialog()` or `useConfirm()` for those).
|
|
28
|
+
*
|
|
29
|
+
* Three size variants (`sm` / `md` / `lg`) with sensible defaults.
|
|
30
|
+
* Backdrop + content animations respect `prefers-reduced-motion`.
|
|
31
|
+
*/
|
|
32
|
+
export interface DialogProps extends Pick<DialogVariants, 'size'> {
|
|
33
|
+
/** Controlled open state. */
|
|
34
|
+
open: boolean;
|
|
35
|
+
/** Called when the dialog requests to change open state. */
|
|
36
|
+
onOpenChange: (open: boolean) => void;
|
|
37
|
+
/** Dialog body — usually a `<DialogContent>` subtree. */
|
|
38
|
+
children: ReactNode;
|
|
39
|
+
/** Dialog title (screen-reader announced via `aria-labelledby`). */
|
|
40
|
+
title?: ReactNode;
|
|
41
|
+
/** Dialog description (announced via `aria-describedby`). */
|
|
42
|
+
description?: ReactNode;
|
|
43
|
+
/**
|
|
44
|
+
* Show the `×` close button in the top-right corner.
|
|
45
|
+
* @default true
|
|
46
|
+
*/
|
|
47
|
+
showCloseButton?: boolean;
|
|
48
|
+
/**
|
|
49
|
+
* Disable closing on backdrop click.
|
|
50
|
+
* @default false
|
|
51
|
+
*/
|
|
52
|
+
disableBackdropClose?: boolean;
|
|
53
|
+
/**
|
|
54
|
+
* Disable closing on Escape key.
|
|
55
|
+
* @default false
|
|
56
|
+
*/
|
|
57
|
+
disableEscapeClose?: boolean;
|
|
58
|
+
/** Root className shortcut on the content element. */
|
|
59
|
+
sx?: string;
|
|
60
|
+
/** Per-slot overrides. */
|
|
61
|
+
slotProps?: DialogSlotProps;
|
|
62
|
+
}
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import { tv, type VariantProps } from 'tailwind-variants';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* `<Dialog>` variant recipe.
|
|
5
|
+
*
|
|
6
|
+
* Slots:
|
|
7
|
+
* - `overlay` — backdrop (Radix's `DialogOverlay`)
|
|
8
|
+
* - `content` — the dialog box itself
|
|
9
|
+
* - `title` — heading at the top
|
|
10
|
+
* - `description` — secondary text under the title
|
|
11
|
+
* - `closeButton` — top-right `×` button
|
|
12
|
+
* - `body` — main scrollable area
|
|
13
|
+
* - `actions` — footer action bar
|
|
14
|
+
*
|
|
15
|
+
* Three sizes (`sm` / `md` / `lg`) drive the content max-width.
|
|
16
|
+
* Motion is gated by `prefers-reduced-motion` so the slide-in
|
|
17
|
+
* animation collapses to an instant appear on users that opt out.
|
|
18
|
+
*/
|
|
19
|
+
export const dialogVariants = tv({
|
|
20
|
+
slots: {
|
|
21
|
+
overlay: [
|
|
22
|
+
'fixed inset-0 z-50 bg-neutral-950/60 backdrop-blur-sm',
|
|
23
|
+
'data-[state=open]:animate-in data-[state=open]:fade-in-0',
|
|
24
|
+
'data-[state=closed]:animate-out data-[state=closed]:fade-out-0',
|
|
25
|
+
'motion-reduce:transition-none motion-reduce:duration-0',
|
|
26
|
+
],
|
|
27
|
+
content: [
|
|
28
|
+
'fixed left-1/2 top-1/2 z-50 -translate-x-1/2 -translate-y-1/2',
|
|
29
|
+
'w-full bg-white dark:bg-neutral-900 rounded-lg shadow-xl',
|
|
30
|
+
'border border-neutral-200 dark:border-neutral-800',
|
|
31
|
+
'p-6 flex flex-col gap-4 max-h-[90vh]',
|
|
32
|
+
'focus:outline-none',
|
|
33
|
+
'data-[state=open]:animate-in data-[state=open]:fade-in-0 data-[state=open]:zoom-in-95',
|
|
34
|
+
'data-[state=closed]:animate-out data-[state=closed]:fade-out-0 data-[state=closed]:zoom-out-95',
|
|
35
|
+
'motion-reduce:transition-none motion-reduce:duration-0',
|
|
36
|
+
],
|
|
37
|
+
title: 'text-lg font-semibold leading-tight text-neutral-900 dark:text-neutral-50',
|
|
38
|
+
description: 'text-sm text-neutral-600 dark:text-neutral-400',
|
|
39
|
+
closeButton: [
|
|
40
|
+
'absolute top-4 right-4 inline-flex items-center justify-center',
|
|
41
|
+
'h-8 w-8 rounded-md text-neutral-500 hover:text-neutral-900',
|
|
42
|
+
'dark:text-neutral-400 dark:hover:text-neutral-50',
|
|
43
|
+
'hover:bg-neutral-100 dark:hover:bg-neutral-800',
|
|
44
|
+
'focus:outline-none focus:ring-2 focus:ring-primary-500',
|
|
45
|
+
'transition-colors',
|
|
46
|
+
],
|
|
47
|
+
body: 'flex-1 overflow-y-auto',
|
|
48
|
+
actions: 'flex justify-end gap-2 pt-2',
|
|
49
|
+
},
|
|
50
|
+
variants: {
|
|
51
|
+
size: {
|
|
52
|
+
sm: { content: 'max-w-sm' },
|
|
53
|
+
md: { content: 'max-w-md' },
|
|
54
|
+
lg: { content: 'max-w-2xl' },
|
|
55
|
+
},
|
|
56
|
+
},
|
|
57
|
+
defaultVariants: {
|
|
58
|
+
size: 'md',
|
|
59
|
+
},
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
export type DialogVariants = VariantProps<typeof dialogVariants>;
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
// @vitest-environment jsdom
|
|
2
|
+
import * as React from 'react';
|
|
3
|
+
import { describe, it, expect, afterEach, beforeAll } from 'vitest';
|
|
4
|
+
import { render, screen, cleanup } from '@testing-library/react';
|
|
5
|
+
import { Popover } from './Popover.js';
|
|
6
|
+
|
|
7
|
+
void React;
|
|
8
|
+
afterEach(() => cleanup());
|
|
9
|
+
|
|
10
|
+
// jsdom doesn't implement ResizeObserver — Radix Floating UI requires it.
|
|
11
|
+
beforeAll(() => {
|
|
12
|
+
if (typeof globalThis.ResizeObserver === 'undefined') {
|
|
13
|
+
(globalThis as unknown as { ResizeObserver: unknown }).ResizeObserver = class {
|
|
14
|
+
observe() {}
|
|
15
|
+
unobserve() {}
|
|
16
|
+
disconnect() {}
|
|
17
|
+
};
|
|
18
|
+
}
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
describe('Popover — rendering', () => {
|
|
22
|
+
it('renders the trigger always', () => {
|
|
23
|
+
render(
|
|
24
|
+
<Popover content={<p>Panel</p>}>
|
|
25
|
+
<button>Open panel</button>
|
|
26
|
+
</Popover>
|
|
27
|
+
);
|
|
28
|
+
expect(screen.queryByRole('button', { name: 'Open panel' })).not.toBeNull();
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
it('renders content when controlled open=true', () => {
|
|
32
|
+
render(
|
|
33
|
+
<Popover content={<p>Panel content</p>} open onOpenChange={() => {}}>
|
|
34
|
+
<button>Open panel</button>
|
|
35
|
+
</Popover>
|
|
36
|
+
);
|
|
37
|
+
expect(screen.queryByText('Panel content')).not.toBeNull();
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
it('does not render content when open=false', () => {
|
|
41
|
+
render(
|
|
42
|
+
<Popover content={<p>Panel content</p>} open={false} onOpenChange={() => {}}>
|
|
43
|
+
<button>Open panel</button>
|
|
44
|
+
</Popover>
|
|
45
|
+
);
|
|
46
|
+
expect(screen.queryByText('Panel content')).toBeNull();
|
|
47
|
+
});
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
describe('Popover — placement', () => {
|
|
51
|
+
it.each(['top', 'right', 'bottom', 'left'] as const)(
|
|
52
|
+
'renders side=%s without crashing',
|
|
53
|
+
(side) => {
|
|
54
|
+
render(
|
|
55
|
+
<Popover content={<p>Panel</p>} open side={side} onOpenChange={() => {}}>
|
|
56
|
+
<button>T</button>
|
|
57
|
+
</Popover>
|
|
58
|
+
);
|
|
59
|
+
expect(screen.queryByText('Panel')).not.toBeNull();
|
|
60
|
+
}
|
|
61
|
+
);
|
|
62
|
+
|
|
63
|
+
it('applies width style', () => {
|
|
64
|
+
render(
|
|
65
|
+
<Popover content={<p>Panel</p>} open width="300px" onOpenChange={() => {}}>
|
|
66
|
+
<button>T</button>
|
|
67
|
+
</Popover>
|
|
68
|
+
);
|
|
69
|
+
const content = screen.getByText('Panel').closest('[role="dialog"]') as HTMLElement | null;
|
|
70
|
+
expect(content?.style.width).toBe('300px');
|
|
71
|
+
});
|
|
72
|
+
});
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import * as RadixPopover from '@radix-ui/react-popover';
|
|
2
|
+
import { cn } from '../../utils/cn.js';
|
|
3
|
+
import { popoverVariants } from './popover.variants.js';
|
|
4
|
+
import type { PopoverProps } from './popover.types.js';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Dashforge TW `<Popover>` — clickable floating content panel.
|
|
8
|
+
*
|
|
9
|
+
* Built on `@radix-ui/react-popover`:
|
|
10
|
+
* - Focus trap inside the popover (Tab cycles within).
|
|
11
|
+
* - Outside-click + Escape dismiss.
|
|
12
|
+
* - APG dialog-like semantics (`role="dialog"` on content).
|
|
13
|
+
* - Portal-rendered.
|
|
14
|
+
*
|
|
15
|
+
* Use `<Popover>` for menus / pickers / settings panels with
|
|
16
|
+
* interactive children. Use `<Tooltip>` for read-only hints.
|
|
17
|
+
*/
|
|
18
|
+
export function Popover(props: PopoverProps) {
|
|
19
|
+
const {
|
|
20
|
+
children,
|
|
21
|
+
content,
|
|
22
|
+
side = 'bottom',
|
|
23
|
+
align = 'center',
|
|
24
|
+
showArrow = false,
|
|
25
|
+
open,
|
|
26
|
+
onOpenChange,
|
|
27
|
+
sideOffset = 8,
|
|
28
|
+
width,
|
|
29
|
+
slotProps,
|
|
30
|
+
} = props;
|
|
31
|
+
|
|
32
|
+
const v = popoverVariants();
|
|
33
|
+
|
|
34
|
+
return (
|
|
35
|
+
<RadixPopover.Root open={open} onOpenChange={onOpenChange}>
|
|
36
|
+
<RadixPopover.Trigger asChild>{children}</RadixPopover.Trigger>
|
|
37
|
+
<RadixPopover.Portal>
|
|
38
|
+
<RadixPopover.Content
|
|
39
|
+
side={side}
|
|
40
|
+
align={align}
|
|
41
|
+
sideOffset={sideOffset}
|
|
42
|
+
style={width != null ? { width } : undefined}
|
|
43
|
+
className={cn(v.content(), slotProps?.content?.className)}
|
|
44
|
+
>
|
|
45
|
+
{content}
|
|
46
|
+
{showArrow && (
|
|
47
|
+
<RadixPopover.Arrow
|
|
48
|
+
className={cn(v.arrow(), slotProps?.arrow?.className)}
|
|
49
|
+
/>
|
|
50
|
+
)}
|
|
51
|
+
</RadixPopover.Content>
|
|
52
|
+
</RadixPopover.Portal>
|
|
53
|
+
</RadixPopover.Root>
|
|
54
|
+
);
|
|
55
|
+
}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import type { ReactNode } from 'react';
|
|
2
|
+
|
|
3
|
+
export interface PopoverSlotProps {
|
|
4
|
+
content?: { className?: string };
|
|
5
|
+
arrow?: { className?: string };
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Props for `<Popover>`.
|
|
10
|
+
*
|
|
11
|
+
* Built on `@radix-ui/react-popover`. Use for richer floating UI
|
|
12
|
+
* than a tooltip — e.g. action menus, color pickers, settings
|
|
13
|
+
* panels.
|
|
14
|
+
*
|
|
15
|
+
* Differences vs `<Tooltip>`:
|
|
16
|
+
* - Popover is **clickable** and captures focus inside (focus trap).
|
|
17
|
+
* - Popover supports arbitrary interactive content (forms, buttons).
|
|
18
|
+
* - Popover dismisses on outside-click / Escape (Radix-managed).
|
|
19
|
+
* - No `role="tooltip"` — popovers are conceptually mini-dialogs.
|
|
20
|
+
*/
|
|
21
|
+
export interface PopoverProps {
|
|
22
|
+
/** The trigger element (wrapped in Radix's `asChild` slot). */
|
|
23
|
+
children: ReactNode;
|
|
24
|
+
/** Popover body content. */
|
|
25
|
+
content: ReactNode;
|
|
26
|
+
/** Side of the trigger to render on. @default 'bottom' */
|
|
27
|
+
side?: 'top' | 'right' | 'bottom' | 'left';
|
|
28
|
+
/** Alignment along the perpendicular axis. @default 'center' */
|
|
29
|
+
align?: 'start' | 'center' | 'end';
|
|
30
|
+
/** Show an arrow indicator. @default false */
|
|
31
|
+
showArrow?: boolean;
|
|
32
|
+
/** Controlled open state. */
|
|
33
|
+
open?: boolean;
|
|
34
|
+
/** Called when open state changes. */
|
|
35
|
+
onOpenChange?: (open: boolean) => void;
|
|
36
|
+
/** Side offset in pixels. @default 8 */
|
|
37
|
+
sideOffset?: number;
|
|
38
|
+
/** Width of the popover content. CSS value. */
|
|
39
|
+
width?: string;
|
|
40
|
+
/** Per-slot overrides. */
|
|
41
|
+
slotProps?: PopoverSlotProps;
|
|
42
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { tv, type VariantProps } from 'tailwind-variants';
|
|
2
|
+
|
|
3
|
+
export const popoverVariants = tv({
|
|
4
|
+
slots: {
|
|
5
|
+
content: [
|
|
6
|
+
'z-50 rounded-md border border-neutral-200 dark:border-neutral-800',
|
|
7
|
+
'bg-white dark:bg-neutral-900 p-4 shadow-lg',
|
|
8
|
+
'text-sm text-neutral-900 dark:text-neutral-50',
|
|
9
|
+
'focus:outline-none',
|
|
10
|
+
'data-[state=open]:animate-in data-[state=open]:fade-in-0 data-[state=open]:zoom-in-95',
|
|
11
|
+
'data-[state=closed]:animate-out data-[state=closed]:fade-out-0 data-[state=closed]:zoom-out-95',
|
|
12
|
+
'motion-reduce:transition-none motion-reduce:duration-0',
|
|
13
|
+
],
|
|
14
|
+
arrow: 'fill-white dark:fill-neutral-900',
|
|
15
|
+
},
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
export type PopoverVariants = VariantProps<typeof popoverVariants>;
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
// @vitest-environment jsdom
|
|
2
|
+
import * as React from 'react';
|
|
3
|
+
import { describe, it, expect, afterEach } from 'vitest';
|
|
4
|
+
import { render, screen, fireEvent, cleanup } from '@testing-library/react';
|
|
5
|
+
import { Tabs } from './Tabs.js';
|
|
6
|
+
|
|
7
|
+
void React;
|
|
8
|
+
afterEach(() => cleanup());
|
|
9
|
+
|
|
10
|
+
const items = [
|
|
11
|
+
{ value: 'overview', label: 'Overview', content: <p>Overview body</p> },
|
|
12
|
+
{ value: 'details', label: 'Details', content: <p>Details body</p> },
|
|
13
|
+
{ value: 'history', label: 'History', content: <p>History body</p> },
|
|
14
|
+
];
|
|
15
|
+
|
|
16
|
+
describe('Tabs — rendering', () => {
|
|
17
|
+
it('renders all triggers and the default-active content', () => {
|
|
18
|
+
render(<Tabs items={items} />);
|
|
19
|
+
expect(screen.queryByRole('tab', { name: 'Overview' })).not.toBeNull();
|
|
20
|
+
expect(screen.queryByRole('tab', { name: 'Details' })).not.toBeNull();
|
|
21
|
+
expect(screen.queryByRole('tab', { name: 'History' })).not.toBeNull();
|
|
22
|
+
// Only the active panel renders content (others get data-state="inactive").
|
|
23
|
+
expect(screen.queryByText('Overview body')).not.toBeNull();
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
it('honors `defaultValue`', () => {
|
|
27
|
+
render(<Tabs items={items} defaultValue="history" />);
|
|
28
|
+
expect(screen.queryByText('History body')).not.toBeNull();
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
it('switches active panel via controlled `value` rerender', () => {
|
|
32
|
+
const { rerender } = render(
|
|
33
|
+
<Tabs items={items} value="overview" onValueChange={() => {}} />
|
|
34
|
+
);
|
|
35
|
+
expect(screen.queryByText('Overview body')).not.toBeNull();
|
|
36
|
+
rerender(<Tabs items={items} value="details" onValueChange={() => {}} />);
|
|
37
|
+
expect(screen.queryByText('Details body')).not.toBeNull();
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
it('marks the active tab via aria-selected', () => {
|
|
41
|
+
render(<Tabs items={items} defaultValue="details" />);
|
|
42
|
+
const detailsTab = screen.getByRole('tab', { name: 'Details' });
|
|
43
|
+
expect(detailsTab.getAttribute('aria-selected')).toBe('true');
|
|
44
|
+
const overviewTab = screen.getByRole('tab', { name: 'Overview' });
|
|
45
|
+
expect(overviewTab.getAttribute('aria-selected')).toBe('false');
|
|
46
|
+
});
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
void fireEvent; // imported for parity but not needed after the controlled-mode simplification
|
|
50
|
+
|
|
51
|
+
describe('Tabs — variants', () => {
|
|
52
|
+
it.each(['underline', 'pill'] as const)('renders variant=%s', (variant) => {
|
|
53
|
+
render(<Tabs items={items} variant={variant} />);
|
|
54
|
+
expect(screen.queryByRole('tablist')).not.toBeNull();
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
it.each(['horizontal', 'vertical'] as const)('renders orientation=%s', (o) => {
|
|
58
|
+
render(<Tabs items={items} orientation={o} />);
|
|
59
|
+
const list = screen.getByRole('tablist');
|
|
60
|
+
expect(list.getAttribute('aria-orientation')).toBe(o);
|
|
61
|
+
});
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
describe('Tabs — disabled items', () => {
|
|
65
|
+
it('marks disabled triggers as disabled', () => {
|
|
66
|
+
const disabledItems = [
|
|
67
|
+
...items.slice(0, 2),
|
|
68
|
+
{ ...items[2]!, disabled: true },
|
|
69
|
+
];
|
|
70
|
+
render(<Tabs items={disabledItems} />);
|
|
71
|
+
const trigger = screen.getByRole('tab', { name: 'History' }) as HTMLButtonElement;
|
|
72
|
+
expect(trigger.disabled).toBe(true);
|
|
73
|
+
});
|
|
74
|
+
});
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import * as RadixTabs from '@radix-ui/react-tabs';
|
|
2
|
+
import { cn } from '../../utils/cn.js';
|
|
3
|
+
import { tabsVariants } from './tabs.variants.js';
|
|
4
|
+
import type { TabsProps } from './tabs.types.js';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Dashforge TW `<Tabs>` — declarative tab navigation.
|
|
8
|
+
*
|
|
9
|
+
* Built on `@radix-ui/react-tabs`:
|
|
10
|
+
* - APG tabs pattern (arrow-key navigation, automatic activation,
|
|
11
|
+
* `role="tablist"` + `role="tab"` + `role="tabpanel"`).
|
|
12
|
+
* - Controlled / uncontrolled (`value` / `defaultValue`).
|
|
13
|
+
*
|
|
14
|
+
* Two variant axes:
|
|
15
|
+
* - `variant` — `underline` (default) | `pill`
|
|
16
|
+
* - `orientation` — `horizontal` (default) | `vertical`
|
|
17
|
+
*/
|
|
18
|
+
export function Tabs(props: TabsProps) {
|
|
19
|
+
const {
|
|
20
|
+
items,
|
|
21
|
+
value,
|
|
22
|
+
defaultValue,
|
|
23
|
+
onValueChange,
|
|
24
|
+
variant = 'underline',
|
|
25
|
+
orientation = 'horizontal',
|
|
26
|
+
sx,
|
|
27
|
+
slotProps,
|
|
28
|
+
} = props;
|
|
29
|
+
|
|
30
|
+
const v = tabsVariants({ variant, orientation });
|
|
31
|
+
const resolvedDefault = defaultValue ?? items[0]?.value;
|
|
32
|
+
|
|
33
|
+
return (
|
|
34
|
+
<RadixTabs.Root
|
|
35
|
+
value={value}
|
|
36
|
+
defaultValue={resolvedDefault}
|
|
37
|
+
onValueChange={onValueChange}
|
|
38
|
+
orientation={orientation}
|
|
39
|
+
className={cn(v.root(), sx, slotProps?.root?.className)}
|
|
40
|
+
>
|
|
41
|
+
<RadixTabs.List
|
|
42
|
+
className={cn(v.list(), slotProps?.list?.className)}
|
|
43
|
+
>
|
|
44
|
+
{items.map((item) => (
|
|
45
|
+
<RadixTabs.Trigger
|
|
46
|
+
key={item.value}
|
|
47
|
+
value={item.value}
|
|
48
|
+
disabled={item.disabled}
|
|
49
|
+
className={cn(v.trigger(), slotProps?.trigger?.className)}
|
|
50
|
+
>
|
|
51
|
+
{item.label}
|
|
52
|
+
</RadixTabs.Trigger>
|
|
53
|
+
))}
|
|
54
|
+
</RadixTabs.List>
|
|
55
|
+
{items.map((item) => (
|
|
56
|
+
<RadixTabs.Content
|
|
57
|
+
key={item.value}
|
|
58
|
+
value={item.value}
|
|
59
|
+
className={cn(v.content(), slotProps?.content?.className)}
|
|
60
|
+
>
|
|
61
|
+
{item.content}
|
|
62
|
+
</RadixTabs.Content>
|
|
63
|
+
))}
|
|
64
|
+
</RadixTabs.Root>
|
|
65
|
+
);
|
|
66
|
+
}
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import type { ReactNode } from 'react';
|
|
2
|
+
import type { TabsVariants } from './tabs.variants.js';
|
|
3
|
+
|
|
4
|
+
export interface TabsSlotProps {
|
|
5
|
+
root?: { className?: string };
|
|
6
|
+
list?: { className?: string };
|
|
7
|
+
trigger?: { className?: string };
|
|
8
|
+
content?: { className?: string };
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Single tab item — paired `{ value, label, content }`. The `value`
|
|
13
|
+
* is the canonical id (selected via the `value` / `defaultValue`
|
|
14
|
+
* props on the root); the `label` is the trigger button text, and
|
|
15
|
+
* `content` is what renders in the panel when this tab is active.
|
|
16
|
+
*/
|
|
17
|
+
export interface TabItem {
|
|
18
|
+
value: string;
|
|
19
|
+
label: ReactNode;
|
|
20
|
+
/** Tab panel content. */
|
|
21
|
+
content: ReactNode;
|
|
22
|
+
/** Per-tab disabled flag. */
|
|
23
|
+
disabled?: boolean;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Props for `<Tabs>`.
|
|
28
|
+
*
|
|
29
|
+
* Built on `@radix-ui/react-tabs`. Two variant axes:
|
|
30
|
+
* - `variant` — `underline` (default) | `pill`
|
|
31
|
+
* - `orientation` — `horizontal` (default) | `vertical`
|
|
32
|
+
*
|
|
33
|
+
* Supports controlled (`value` + `onValueChange`) and uncontrolled
|
|
34
|
+
* (`defaultValue`) modes — the `value` prop wins when both are set.
|
|
35
|
+
*/
|
|
36
|
+
export interface TabsProps extends TabsVariants {
|
|
37
|
+
/** Tab items array (declarative — alternative to passing children). */
|
|
38
|
+
items: TabItem[];
|
|
39
|
+
/** Controlled active value. */
|
|
40
|
+
value?: string;
|
|
41
|
+
/** Default active value (uncontrolled). Defaults to first item's value. */
|
|
42
|
+
defaultValue?: string;
|
|
43
|
+
/** Fires when the active tab changes. */
|
|
44
|
+
onValueChange?: (value: string) => void;
|
|
45
|
+
/** Root className shortcut. */
|
|
46
|
+
sx?: string;
|
|
47
|
+
/** Per-slot overrides. */
|
|
48
|
+
slotProps?: TabsSlotProps;
|
|
49
|
+
}
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import { tv, type VariantProps } from 'tailwind-variants';
|
|
2
|
+
|
|
3
|
+
export const tabsVariants = tv({
|
|
4
|
+
slots: {
|
|
5
|
+
root: 'flex',
|
|
6
|
+
list: 'inline-flex items-center gap-1',
|
|
7
|
+
trigger: [
|
|
8
|
+
'inline-flex items-center justify-center whitespace-nowrap',
|
|
9
|
+
'text-sm font-medium transition-colors',
|
|
10
|
+
'focus:outline-none focus-visible:ring-2 focus-visible:ring-primary-500',
|
|
11
|
+
'disabled:opacity-50 disabled:pointer-events-none',
|
|
12
|
+
'motion-reduce:transition-none',
|
|
13
|
+
],
|
|
14
|
+
content: [
|
|
15
|
+
'mt-3 focus:outline-none',
|
|
16
|
+
'data-[state=inactive]:hidden',
|
|
17
|
+
],
|
|
18
|
+
},
|
|
19
|
+
variants: {
|
|
20
|
+
variant: {
|
|
21
|
+
underline: {
|
|
22
|
+
list: 'border-b border-neutral-200 dark:border-neutral-800',
|
|
23
|
+
trigger: [
|
|
24
|
+
'px-3 py-2 border-b-2 border-transparent text-neutral-600 dark:text-neutral-400',
|
|
25
|
+
'hover:text-neutral-900 dark:hover:text-neutral-50',
|
|
26
|
+
'data-[state=active]:border-primary-500 data-[state=active]:text-primary-700',
|
|
27
|
+
'dark:data-[state=active]:text-primary-400',
|
|
28
|
+
],
|
|
29
|
+
},
|
|
30
|
+
pill: {
|
|
31
|
+
list: 'rounded-lg bg-neutral-100 dark:bg-neutral-800 p-1',
|
|
32
|
+
trigger: [
|
|
33
|
+
'px-3 py-1.5 rounded-md text-neutral-600 dark:text-neutral-400',
|
|
34
|
+
'hover:text-neutral-900 dark:hover:text-neutral-50',
|
|
35
|
+
'data-[state=active]:bg-white dark:data-[state=active]:bg-neutral-900',
|
|
36
|
+
'data-[state=active]:text-neutral-900 dark:data-[state=active]:text-neutral-50',
|
|
37
|
+
'data-[state=active]:shadow-sm',
|
|
38
|
+
],
|
|
39
|
+
},
|
|
40
|
+
},
|
|
41
|
+
orientation: {
|
|
42
|
+
horizontal: {
|
|
43
|
+
root: 'flex-col',
|
|
44
|
+
list: 'flex-row',
|
|
45
|
+
},
|
|
46
|
+
vertical: {
|
|
47
|
+
root: 'flex-row gap-4',
|
|
48
|
+
list: 'flex-col items-stretch',
|
|
49
|
+
},
|
|
50
|
+
},
|
|
51
|
+
},
|
|
52
|
+
defaultVariants: {
|
|
53
|
+
variant: 'underline',
|
|
54
|
+
orientation: 'horizontal',
|
|
55
|
+
},
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
export type TabsVariants = VariantProps<typeof tabsVariants>;
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
// @vitest-environment jsdom
|
|
2
|
+
import * as React from 'react';
|
|
3
|
+
import { describe, it, expect, afterEach, beforeAll } from 'vitest';
|
|
4
|
+
import { render, screen, cleanup } from '@testing-library/react';
|
|
5
|
+
import { Tooltip } from './Tooltip.js';
|
|
6
|
+
|
|
7
|
+
void React;
|
|
8
|
+
afterEach(() => cleanup());
|
|
9
|
+
|
|
10
|
+
// jsdom doesn't implement ResizeObserver — Radix Floating UI requires it.
|
|
11
|
+
// Shim with a noop before any test mounts a Tooltip.
|
|
12
|
+
beforeAll(() => {
|
|
13
|
+
if (typeof globalThis.ResizeObserver === 'undefined') {
|
|
14
|
+
(globalThis as unknown as { ResizeObserver: unknown }).ResizeObserver = class {
|
|
15
|
+
observe() {}
|
|
16
|
+
unobserve() {}
|
|
17
|
+
disconnect() {}
|
|
18
|
+
};
|
|
19
|
+
}
|
|
20
|
+
if (typeof globalThis.DOMRect === 'undefined') {
|
|
21
|
+
(globalThis as unknown as { DOMRect: unknown }).DOMRect = class {
|
|
22
|
+
static fromRect(r?: DOMRectInit) {
|
|
23
|
+
return new (globalThis as unknown as { DOMRect: new (x: number, y: number, w: number, h: number) => unknown }).DOMRect(
|
|
24
|
+
r?.x ?? 0, r?.y ?? 0, r?.width ?? 0, r?.height ?? 0
|
|
25
|
+
);
|
|
26
|
+
}
|
|
27
|
+
constructor(public x = 0, public y = 0, public width = 0, public height = 0) {}
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
describe('Tooltip — rendering', () => {
|
|
33
|
+
it('renders the trigger (children) always', () => {
|
|
34
|
+
render(
|
|
35
|
+
<Tooltip content="Tip text">
|
|
36
|
+
<button>Trigger</button>
|
|
37
|
+
</Tooltip>
|
|
38
|
+
);
|
|
39
|
+
expect(screen.queryByRole('button', { name: 'Trigger' })).not.toBeNull();
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
it('renders content when controlled open=true', () => {
|
|
43
|
+
render(
|
|
44
|
+
<Tooltip content="Tip text" open onOpenChange={() => {}}>
|
|
45
|
+
<button>Trigger</button>
|
|
46
|
+
</Tooltip>
|
|
47
|
+
);
|
|
48
|
+
expect(screen.queryAllByText('Tip text').length).toBeGreaterThan(0);
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
it('does not render content when controlled open=false', () => {
|
|
52
|
+
render(
|
|
53
|
+
<Tooltip content="Tip text" open={false} onOpenChange={() => {}}>
|
|
54
|
+
<button>Trigger</button>
|
|
55
|
+
</Tooltip>
|
|
56
|
+
);
|
|
57
|
+
expect(screen.queryByText('Tip text')).toBeNull();
|
|
58
|
+
});
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
describe('Tooltip — placement props', () => {
|
|
62
|
+
it.each(['top', 'right', 'bottom', 'left'] as const)(
|
|
63
|
+
'renders side=%s without crashing',
|
|
64
|
+
(side) => {
|
|
65
|
+
render(
|
|
66
|
+
<Tooltip content="Tip" open side={side} onOpenChange={() => {}}>
|
|
67
|
+
<button>Trigger</button>
|
|
68
|
+
</Tooltip>
|
|
69
|
+
);
|
|
70
|
+
expect(screen.queryByRole('button', { name: 'Trigger' })).not.toBeNull();
|
|
71
|
+
}
|
|
72
|
+
);
|
|
73
|
+
});
|