@tangible/ui 0.0.17 → 0.0.18

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.
@@ -0,0 +1,21 @@
1
+ import type { PopoverProps, PopoverTriggerProps, PopoverContentProps } from './types';
2
+ declare function PopoverRoot({ open: controlledOpen, onOpenChange, defaultOpen, children, }: PopoverProps): import("react/jsx-runtime").JSX.Element;
3
+ declare namespace PopoverRoot {
4
+ var displayName: string;
5
+ }
6
+ declare function PopoverTriggerComponent({ asChild, children }: PopoverTriggerProps): import("react/jsx-runtime").JSX.Element;
7
+ declare namespace PopoverTriggerComponent {
8
+ var displayName: string;
9
+ }
10
+ declare function PopoverContentComponent(props: PopoverContentProps): import("react/jsx-runtime").JSX.Element | null;
11
+ declare namespace PopoverContentComponent {
12
+ var displayName: string;
13
+ }
14
+ type PopoverCompound = typeof PopoverRoot & {
15
+ Trigger: typeof PopoverTriggerComponent;
16
+ Content: typeof PopoverContentComponent;
17
+ };
18
+ export declare const Popover: PopoverCompound;
19
+ export declare const PopoverTrigger: typeof PopoverTriggerComponent;
20
+ export declare const PopoverContent: typeof PopoverContentComponent;
21
+ export { usePopoverContext as usePopover } from './PopoverContext';
@@ -0,0 +1,148 @@
1
+ import { jsx as _jsx } from "react/jsx-runtime";
2
+ import React, { useCallback, useEffect, useId, useMemo, useRef, useState, cloneElement, isValidElement, } from 'react';
3
+ import { useFloating, offset, flip, shift, autoUpdate, FloatingPortal, useDismiss, useInteractions, useRole, FloatingFocusManager, } from '@floating-ui/react';
4
+ import { cx } from '../../utils/cx.js';
5
+ import { getPortalRootFor } from '../../utils/portal.js';
6
+ import { PopoverContext, usePopoverContext } from './PopoverContext.js';
7
+ import { toPlacement } from './types.js';
8
+ // =============================================================================
9
+ // Popover
10
+ // =============================================================================
11
+ //
12
+ // An anchored floating panel for arbitrary content — the dialog-flavored
13
+ // sibling of Dropdown. Dropdown owns menu semantics (menuitem roles, list
14
+ // navigation); Popover owns everything that isn't a menu: date pickers,
15
+ // search comboboxes, small forms, rich previews. Content renders in a
16
+ // portal inside the nearest `.tui-interface` scope (token inheritance,
17
+ // admin z-index isolation), dismisses on Esc/outside-press, and returns
18
+ // focus to the trigger on close.
19
+ //
20
+ // =============================================================================
21
+ // =============================================================================
22
+ // Popover Root
23
+ // =============================================================================
24
+ function PopoverRoot({ open: controlledOpen, onOpenChange, defaultOpen = false, children, }) {
25
+ const [uncontrolledOpen, setUncontrolledOpen] = useState(defaultOpen);
26
+ const isControlled = controlledOpen !== undefined;
27
+ const open = isControlled ? controlledOpen : uncontrolledOpen;
28
+ const setOpen = useCallback((nextOpen) => {
29
+ if (!isControlled) {
30
+ setUncontrolledOpen(nextOpen);
31
+ }
32
+ onOpenChange?.(nextOpen);
33
+ }, [isControlled, onOpenChange]);
34
+ const triggerRef = useRef(null);
35
+ const contentId = useId();
36
+ // Focus restoration: return focus to the trigger when the popover closes
37
+ // after having been open (parity with Dropdown).
38
+ const shouldRestoreFocus = useRef(false);
39
+ const prevOpen = useRef(open);
40
+ useEffect(() => {
41
+ if (open && !prevOpen.current) {
42
+ shouldRestoreFocus.current = true;
43
+ }
44
+ if (!open && prevOpen.current && shouldRestoreFocus.current) {
45
+ triggerRef.current?.focus();
46
+ shouldRestoreFocus.current = false;
47
+ }
48
+ prevOpen.current = open;
49
+ }, [open]);
50
+ const contextValue = useMemo(() => ({ open, setOpen, triggerRef, contentId }), [open, setOpen, contentId]);
51
+ return _jsx(PopoverContext.Provider, { value: contextValue, children: children });
52
+ }
53
+ PopoverRoot.displayName = 'Popover';
54
+ // =============================================================================
55
+ // PopoverTrigger
56
+ // =============================================================================
57
+ function PopoverTriggerComponent({ asChild = false, children }) {
58
+ const { open, setOpen, triggerRef, contentId } = usePopoverContext();
59
+ const handleClick = useCallback(() => {
60
+ setOpen(!open);
61
+ }, [open, setOpen]);
62
+ const handleKeyDown = useCallback((e) => {
63
+ if (e.key === 'Enter' || e.key === ' ') {
64
+ e.preventDefault();
65
+ setOpen(!open);
66
+ }
67
+ }, [open, setOpen]);
68
+ // Merge child ref with our triggerRef
69
+ const setRefs = useCallback((node) => {
70
+ triggerRef.current = node;
71
+ if (asChild && isValidElement(children)) {
72
+ const childRef = children.ref;
73
+ if (typeof childRef === 'function') {
74
+ childRef(node);
75
+ }
76
+ else if (childRef && typeof childRef === 'object') {
77
+ childRef.current = node;
78
+ }
79
+ }
80
+ }, [asChild, children, triggerRef]);
81
+ const triggerProps = {
82
+ onClick: handleClick,
83
+ 'aria-haspopup': 'dialog',
84
+ 'aria-expanded': open,
85
+ 'aria-controls': open ? contentId : undefined,
86
+ 'data-popover-open': open || undefined,
87
+ };
88
+ if (asChild && isValidElement(children)) {
89
+ return cloneElement(children, {
90
+ ...triggerProps,
91
+ ref: setRefs,
92
+ });
93
+ }
94
+ // Non-asChild wrapper needs proper button semantics (and key handling —
95
+ // asChild buttons bring their own).
96
+ return (_jsx("span", { ref: setRefs, role: "button", tabIndex: 0, onKeyDown: handleKeyDown, ...triggerProps, style: { display: 'inline-flex' }, children: children }));
97
+ }
98
+ PopoverTriggerComponent.displayName = 'Popover.Trigger';
99
+ // =============================================================================
100
+ // PopoverContent
101
+ // =============================================================================
102
+ // Gate component: mounts the real content (and all Floating UI hooks)
103
+ // only while open — parity with Dropdown's gate pattern.
104
+ function PopoverContentComponent(props) {
105
+ const { open } = usePopoverContext();
106
+ if (!open)
107
+ return null;
108
+ return _jsx(PopoverContentInner, { ...props });
109
+ }
110
+ PopoverContentComponent.displayName = 'Popover.Content';
111
+ function PopoverContentInner({ side = 'bottom', align = 'start', sideOffset = 4, modal = false, initialFocus = 0, 'aria-label': ariaLabel, className, style, children, }) {
112
+ const { setOpen, triggerRef, contentId } = usePopoverContext();
113
+ const { refs, floatingStyles, context } = useFloating({
114
+ placement: toPlacement(side, align),
115
+ open: true, // Always true when mounted (gate handles the conditional)
116
+ onOpenChange: setOpen,
117
+ middleware: [offset(sideOffset), flip(), shift({ padding: 8 })],
118
+ whileElementsMounted: autoUpdate,
119
+ });
120
+ // Sync reference element
121
+ useEffect(() => {
122
+ if (triggerRef.current) {
123
+ refs.setReference(triggerRef.current);
124
+ }
125
+ }, [triggerRef, refs]);
126
+ const dismiss = useDismiss(context);
127
+ const role = useRole(context, { role: 'dialog' });
128
+ const { getFloatingProps } = useInteractions([dismiss, role]);
129
+ // Get portal root inside .tui-interface
130
+ const portalRoot = getPortalRootFor(triggerRef.current);
131
+ return (_jsx(FloatingPortal, { root: portalRoot, children: _jsx(FloatingFocusManager, { context: context, modal: modal, initialFocus: initialFocus, children: _jsx("div", { ref: refs.setFloating, "aria-label": ariaLabel, className: cx('tui-popover', className), style: {
132
+ ...floatingStyles,
133
+ ...style,
134
+ }, ...getFloatingProps(),
135
+ // After the spread: useRole supplies its own id, but the trigger's
136
+ // aria-controls points at contentId — they must match.
137
+ id: contentId, children: children }) }) }));
138
+ }
139
+ export const Popover = PopoverRoot;
140
+ Popover.Trigger = PopoverTriggerComponent;
141
+ Popover.Content = PopoverContentComponent;
142
+ // Named exports for direct imports
143
+ export const PopoverTrigger = PopoverTriggerComponent;
144
+ export const PopoverContent = PopoverContentComponent;
145
+ // Hook for advanced use cases (content that needs to close the popover,
146
+ // e.g. a picker closing itself after selection)
147
+ // eslint-disable-next-line react-refresh/only-export-components
148
+ export { usePopoverContext as usePopover } from './PopoverContext.js';
@@ -0,0 +1,3 @@
1
+ import type { PopoverContextValue } from './types';
2
+ export declare const PopoverContext: import("react").Context<PopoverContextValue | null>;
3
+ export declare function usePopoverContext(): PopoverContextValue;
@@ -0,0 +1,9 @@
1
+ import { createContext, useContext } from 'react';
2
+ export const PopoverContext = createContext(null);
3
+ export function usePopoverContext() {
4
+ const context = useContext(PopoverContext);
5
+ if (!context) {
6
+ throw new Error('Popover components must be used within a Popover');
7
+ }
8
+ return context;
9
+ }
@@ -0,0 +1,2 @@
1
+ export { Popover, PopoverTrigger, PopoverContent, usePopover } from './Popover';
2
+ export type { PopoverProps, PopoverTriggerProps, PopoverContentProps, } from './types';
@@ -0,0 +1 @@
1
+ export { Popover, PopoverTrigger, PopoverContent, usePopover } from './Popover.js';
@@ -0,0 +1,83 @@
1
+ import type { Placement } from '@floating-ui/react';
2
+ import type { CSSProperties, ReactNode, RefObject } from 'react';
3
+ /**
4
+ * Side of the trigger the popover prefers (flips when there's no room).
5
+ */
6
+ export type Side = 'top' | 'right' | 'bottom' | 'left';
7
+ /**
8
+ * Alignment along the chosen side.
9
+ */
10
+ export type Align = 'start' | 'center' | 'end';
11
+ export type PopoverProps = {
12
+ /**
13
+ * Controlled open state.
14
+ */
15
+ open?: boolean;
16
+ /**
17
+ * Default open state for uncontrolled usage.
18
+ * @default false
19
+ */
20
+ defaultOpen?: boolean;
21
+ /**
22
+ * Callback when open state changes.
23
+ */
24
+ onOpenChange?: (open: boolean) => void;
25
+ children: ReactNode;
26
+ };
27
+ export type PopoverTriggerProps = {
28
+ /**
29
+ * Merge trigger behavior into the child element instead of wrapping it.
30
+ * The child must accept ref and event props (e.g. Button, IconButton).
31
+ * @default false
32
+ */
33
+ asChild?: boolean;
34
+ children: ReactNode;
35
+ };
36
+ export type PopoverContentProps = {
37
+ /**
38
+ * Side of the trigger to render against.
39
+ * @default 'bottom'
40
+ */
41
+ side?: Side;
42
+ /**
43
+ * Alignment along the side.
44
+ * @default 'start'
45
+ */
46
+ align?: Align;
47
+ /**
48
+ * Gap between trigger and content, in px.
49
+ * @default 4
50
+ */
51
+ sideOffset?: number;
52
+ /**
53
+ * Trap focus inside the popover while open. Use for content with
54
+ * multiple interactive controls (forms, pickers); leave off for
55
+ * light informational content.
56
+ * @default false
57
+ */
58
+ modal?: boolean;
59
+ /**
60
+ * Initial focus target: 0 focuses the first tabbable element inside
61
+ * the popover, -1 leaves focus on the trigger.
62
+ * @default 0
63
+ */
64
+ initialFocus?: number;
65
+ /**
66
+ * Accessible name for the dialog. Provide this (or aria-labelledby
67
+ * via a heading inside) so the popover announces meaningfully.
68
+ */
69
+ 'aria-label'?: string;
70
+ className?: string;
71
+ style?: CSSProperties;
72
+ children: ReactNode;
73
+ };
74
+ export type PopoverContextValue = {
75
+ open: boolean;
76
+ setOpen: (open: boolean) => void;
77
+ triggerRef: RefObject<HTMLElement | null>;
78
+ contentId: string;
79
+ };
80
+ /**
81
+ * Map side + align to a Floating UI placement.
82
+ */
83
+ export declare function toPlacement(side: Side, align: Align): Placement;
@@ -0,0 +1,11 @@
1
+ // -----------------------------------------------------------------------------
2
+ // Helpers
3
+ // -----------------------------------------------------------------------------
4
+ /**
5
+ * Map side + align to a Floating UI placement.
6
+ */
7
+ export function toPlacement(side, align) {
8
+ if (align === 'center')
9
+ return side;
10
+ return `${side}-${align}`;
11
+ }
@@ -22,6 +22,7 @@ export { MoveHandle } from './MoveHandle';
22
22
  export { Notice } from './Notice';
23
23
  export { OverlapStack } from './OverlapStack';
24
24
  export { Pager } from './Pager';
25
+ export { Popover, PopoverTrigger, PopoverContent, usePopover } from './Popover';
25
26
  export { Progress } from './Progress';
26
27
  export { RadioGroup, Radio } from './Radio';
27
28
  export { Rating } from './Rating';
@@ -58,6 +59,7 @@ export type { MoveHandleLabels, MoveHandleMode, MoveHandleProps, MoveHandleSize
58
59
  export type { NoticeProps } from './Notice';
59
60
  export type { OverlapStackProps, OverlapStackOverflowProps } from './OverlapStack';
60
61
  export type { PagerProps, PagerMode } from './Pager';
62
+ export type { PopoverProps, PopoverTriggerProps, PopoverContentProps, } from './Popover';
61
63
  export type { ProgressProps } from './Progress';
62
64
  export type { RadioGroupProps, RadioProps } from './Radio';
63
65
  export type { RatingProps } from './Rating';
@@ -20,6 +20,7 @@ export { MoveHandle } from './MoveHandle/index.js';
20
20
  export { Notice } from './Notice/index.js';
21
21
  export { OverlapStack } from './OverlapStack/index.js';
22
22
  export { Pager } from './Pager/index.js';
23
+ export { Popover, PopoverTrigger, PopoverContent, usePopover } from './Popover/index.js';
23
24
  export { Progress } from './Progress/index.js';
24
25
  export { RadioGroup, Radio } from './Radio/index.js';
25
26
  export { Rating } from './Rating/index.js';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tangible/ui",
3
- "version": "0.0.17",
3
+ "version": "0.0.18",
4
4
  "description": "Tangible Design System",
5
5
  "type": "module",
6
6
  "main": "./components/index.js",
@@ -126,6 +126,10 @@
126
126
  "import": "./components/Pager/index.js",
127
127
  "types": "./components/Pager/index.d.ts"
128
128
  },
129
+ "./components/Popover": {
130
+ "import": "./components/Popover/index.js",
131
+ "types": "./components/Popover/index.d.ts"
132
+ },
129
133
  "./components/Progress": {
130
134
  "import": "./components/Progress/index.js",
131
135
  "types": "./components/Progress/index.d.ts"