@plexui/ui 0.2.1 → 0.4.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/dist/es/components/Field/Field.js +34 -0
- package/dist/es/components/Field/Field.js.map +1 -0
- package/dist/es/components/Field/Field.module.css +97 -0
- package/dist/es/components/Field/index.js +2 -0
- package/dist/es/components/Field/index.js.map +1 -0
- package/dist/es/components/SegmentedControl/index.js +1 -1
- package/dist/es/components/SegmentedControl/index.js.map +1 -1
- package/dist/es/components/Slider/Slider.js +68 -37
- package/dist/es/components/Slider/Slider.js.map +1 -1
- package/dist/es/components/Slider/Slider.module.css +36 -3
- package/dist/es/components/Tabs/Tabs.js +153 -0
- package/dist/es/components/Tabs/Tabs.js.map +1 -0
- package/dist/es/components/{SegmentedControl/SegmentedControl.module.css → Tabs/Tabs.module.css} +232 -98
- package/dist/es/components/Tabs/index.js +2 -0
- package/dist/es/components/Tabs/index.js.map +1 -0
- package/dist/es/styles/variables-components.css +12 -0
- package/dist/types/components/Field/Field.d.ts +58 -0
- package/dist/types/components/Field/index.d.ts +1 -0
- package/dist/types/components/SegmentedControl/index.d.ts +2 -2
- package/dist/types/components/Slider/Slider.d.ts +47 -18
- package/dist/types/components/{SegmentedControl/SegmentedControl.d.ts → Tabs/Tabs.d.ts} +30 -14
- package/dist/types/components/Tabs/index.d.ts +2 -0
- package/package.json +1 -1
- package/dist/es/components/SegmentedControl/SegmentedControl.js +0 -118
- package/dist/es/components/SegmentedControl/SegmentedControl.js.map +0 -1
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
3
|
+
import clsx from "clsx";
|
|
4
|
+
import { ToggleGroup } from "radix-ui";
|
|
5
|
+
import { useCallback, useLayoutEffect, useRef } from "react";
|
|
6
|
+
import { useResizeObserver } from "usehooks-ts";
|
|
7
|
+
import { handlePressableMouseEnter, waitForAnimationFrame } from "../../lib/helpers";
|
|
8
|
+
import {} from "../../types";
|
|
9
|
+
import { LoadingIndicator } from "../Indicator";
|
|
10
|
+
import s from "./Tabs.module.css";
|
|
11
|
+
export const Tabs = ({ value, onChange, children, variant = "segmented", orientation = "horizontal", block, pill = true, size = "md", gutterSize, className, onClick, ...restProps }) => {
|
|
12
|
+
const rootRef = useRef(null);
|
|
13
|
+
const thumbRef = useRef(null);
|
|
14
|
+
const prevSizeRef = useRef(size);
|
|
15
|
+
const isVertical = orientation === "vertical";
|
|
16
|
+
const applyThumbSizing = useCallback((attemptScroll) => {
|
|
17
|
+
const root = rootRef.current;
|
|
18
|
+
const thumb = thumbRef.current;
|
|
19
|
+
if (!root || !thumb) {
|
|
20
|
+
return;
|
|
21
|
+
}
|
|
22
|
+
// Get selected node
|
|
23
|
+
const activeNode = root?.querySelector('[data-state="on"]');
|
|
24
|
+
// Impossible
|
|
25
|
+
if (!activeNode) {
|
|
26
|
+
return;
|
|
27
|
+
}
|
|
28
|
+
if (isVertical) {
|
|
29
|
+
const rootHeight = root.clientHeight;
|
|
30
|
+
let targetHeight = Math.floor(activeNode.clientHeight);
|
|
31
|
+
const targetOffset = activeNode.offsetTop;
|
|
32
|
+
// Detect subpixel edge case
|
|
33
|
+
if (rootHeight - (targetHeight + targetOffset) < 2) {
|
|
34
|
+
targetHeight = targetHeight - 1;
|
|
35
|
+
}
|
|
36
|
+
thumb.style.height = `${Math.floor(targetHeight)}px`;
|
|
37
|
+
thumb.style.width = "";
|
|
38
|
+
thumb.style.transform = `translateY(${targetOffset}px)`;
|
|
39
|
+
// Scroll into view if needed
|
|
40
|
+
if (root.scrollHeight > rootHeight) {
|
|
41
|
+
const buffer = rootHeight * 0.15;
|
|
42
|
+
const scrollTop = root.scrollTop;
|
|
43
|
+
const top = activeNode.offsetTop;
|
|
44
|
+
const bottom = top + targetHeight;
|
|
45
|
+
if (top < scrollTop + buffer || bottom > scrollTop + rootHeight - buffer) {
|
|
46
|
+
if (attemptScroll) {
|
|
47
|
+
activeNode.scrollIntoView({ block: "center", inline: "nearest", behavior: "smooth" });
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
else {
|
|
53
|
+
const rootWidth = root.clientWidth;
|
|
54
|
+
let targetWidth = Math.floor(activeNode.clientWidth);
|
|
55
|
+
const targetOffset = activeNode.offsetLeft;
|
|
56
|
+
// Detect if the thumb is moving too far to the edge of the container.
|
|
57
|
+
// This would most commonly be due to subpixel widths adding up to excessive distance.
|
|
58
|
+
if (rootWidth - (targetWidth + targetOffset) < 2) {
|
|
59
|
+
targetWidth = targetWidth - 1;
|
|
60
|
+
}
|
|
61
|
+
thumb.style.width = `${Math.floor(targetWidth)}px`;
|
|
62
|
+
thumb.style.height = "";
|
|
63
|
+
thumb.style.transform = `translateX(${targetOffset}px)`;
|
|
64
|
+
// If the control is scrollable, ensure the active option is visible
|
|
65
|
+
if (root.scrollWidth > rootWidth) {
|
|
66
|
+
// Only scroll items near the edge, but not the inner 2/3.
|
|
67
|
+
const buffer = rootWidth * 0.15;
|
|
68
|
+
const scrollLeft = root.scrollLeft;
|
|
69
|
+
const left = activeNode.offsetLeft;
|
|
70
|
+
const right = left + targetWidth;
|
|
71
|
+
if (left < scrollLeft + buffer || right > scrollLeft + rootWidth - buffer) {
|
|
72
|
+
// Cheap trick to avoid unintentional scroll on mount - transition is set after mounting
|
|
73
|
+
if (attemptScroll) {
|
|
74
|
+
activeNode.scrollIntoView({ block: "nearest", inline: "center", behavior: "smooth" });
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
}, [isVertical]);
|
|
80
|
+
useResizeObserver({
|
|
81
|
+
// @ts-expect-error(2322) -- bug in types: https://github.com/juliencrn/usehooks-ts/issues/663
|
|
82
|
+
ref: rootRef,
|
|
83
|
+
onResize: () => {
|
|
84
|
+
const thumb = thumbRef.current;
|
|
85
|
+
if (!thumb) {
|
|
86
|
+
return;
|
|
87
|
+
}
|
|
88
|
+
// Perform the size update instantly
|
|
89
|
+
const currentTransition = thumb.style.transition;
|
|
90
|
+
thumb.style.transition = "";
|
|
91
|
+
applyThumbSizing(false);
|
|
92
|
+
thumb.style.transition = currentTransition;
|
|
93
|
+
},
|
|
94
|
+
});
|
|
95
|
+
const transitionProperty = isVertical
|
|
96
|
+
? "height 300ms var(--cubic-enter), transform 300ms var(--cubic-enter)"
|
|
97
|
+
: "width 300ms var(--cubic-enter), transform 300ms var(--cubic-enter)";
|
|
98
|
+
useLayoutEffect(() => {
|
|
99
|
+
const root = rootRef.current;
|
|
100
|
+
const thumb = thumbRef.current;
|
|
101
|
+
if (!root || !thumb) {
|
|
102
|
+
return;
|
|
103
|
+
}
|
|
104
|
+
const sizeChanged = prevSizeRef.current !== size;
|
|
105
|
+
prevSizeRef.current = size;
|
|
106
|
+
if (sizeChanged) {
|
|
107
|
+
// Size changed - disable transition, wait for CSS, then apply sizing
|
|
108
|
+
const currentTransition = thumb.style.transition;
|
|
109
|
+
thumb.style.transition = "";
|
|
110
|
+
waitForAnimationFrame(() => {
|
|
111
|
+
applyThumbSizing(false);
|
|
112
|
+
waitForAnimationFrame(() => {
|
|
113
|
+
thumb.style.transition = currentTransition;
|
|
114
|
+
});
|
|
115
|
+
});
|
|
116
|
+
}
|
|
117
|
+
else {
|
|
118
|
+
// Normal update (value change, etc.)
|
|
119
|
+
waitForAnimationFrame(() => {
|
|
120
|
+
applyThumbSizing(!!thumb.style.transition);
|
|
121
|
+
// Apply transition after initial calculation is set
|
|
122
|
+
if (!thumb.style.transition) {
|
|
123
|
+
waitForAnimationFrame(() => {
|
|
124
|
+
thumb.style.transition = transitionProperty;
|
|
125
|
+
});
|
|
126
|
+
}
|
|
127
|
+
});
|
|
128
|
+
}
|
|
129
|
+
}, [applyThumbSizing, value, size, gutterSize, pill, transitionProperty]);
|
|
130
|
+
const handleValueChange = (nextValue) => {
|
|
131
|
+
// Only trigger onChange when a value exists
|
|
132
|
+
// Disallow toggling off enabled items
|
|
133
|
+
if (nextValue && onChange)
|
|
134
|
+
onChange(nextValue);
|
|
135
|
+
};
|
|
136
|
+
// Only apply pill for segmented variant
|
|
137
|
+
const isPill = variant === "segmented" && pill;
|
|
138
|
+
return (_jsxs(ToggleGroup.Root, { ref: rootRef, className: clsx(s.Tabs, className), type: "single", value: value, loop: false, onValueChange: handleValueChange, onClick: onClick, "data-variant": variant, "data-orientation": orientation, "data-block": block ? "" : undefined, "data-pill": isPill ? "" : undefined, "data-size": size, "data-gutter-size": gutterSize, ...restProps, children: [_jsx("div", { className: s.TabsThumb, ref: thumbRef }), children] }));
|
|
139
|
+
};
|
|
140
|
+
// Type guard for badge object form
|
|
141
|
+
const isBadgeObject = (badge) => {
|
|
142
|
+
return badge != null && typeof badge === "object" && "content" in badge;
|
|
143
|
+
};
|
|
144
|
+
const Tab = ({ children, icon, badge, ...restProps }) => {
|
|
145
|
+
// Normalize badge prop
|
|
146
|
+
const badgeProps = badge != null ? (isBadgeObject(badge) ? badge : { content: badge }) : null;
|
|
147
|
+
return (_jsx(ToggleGroup.Item, { className: s.Tab, ...restProps, onPointerEnter: handlePressableMouseEnter, children: _jsxs("span", { className: s.TabContent, children: [icon, children && _jsx("span", { children: children }), badgeProps && (_jsx("span", { className: s.TabBadge, "data-color": badgeProps.color ?? "secondary", "data-variant": badgeProps.variant ?? "soft", "data-pill": badgeProps.pill ? "" : undefined, children: badgeProps.loading ? _jsx(LoadingIndicator, {}) : badgeProps.content }))] }) }));
|
|
148
|
+
};
|
|
149
|
+
// Attach sub-components
|
|
150
|
+
Tabs.Tab = Tab;
|
|
151
|
+
// Backward-compat alias
|
|
152
|
+
Tabs.Option = Tab;
|
|
153
|
+
//# sourceMappingURL=Tabs.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Tabs.js","sourceRoot":"","sources":["../../../../src/components/Tabs/Tabs.tsx"],"names":[],"mappings":"AAAA,YAAY,CAAA;;AAEZ,OA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ui\"\nimport { useCallback, useLayoutEffect, useRef } from \"react\"\nimport { useResizeObserver } from \"usehooks-ts\"\nimport { handlePressableMouseEnter, waitForAnimationFrame } from \"../../lib/helpers\"\nimport { type ControlSize, type SemanticColors, type Sizes, type Variants } from \"../../types\"\nimport { LoadingIndicator } from \"../Indicator\"\nimport s from \"./Tabs.module.css\"\n\nexport type SizeVariant = \"2xs\" | \"xs\" | \"sm\" | \"md\" | \"lg\" | \"xl\"\n\nexport type TabsVariant = \"segmented\" | \"underline\"\nexport type TabsOrientation = \"horizontal\" | \"vertical\"\n\nexport type TabsProps<T extends string> = {\n /**\n * Controlled value for the group\n */\n \"value\": T\n /** Callback for when a new value is selected */\n \"onChange\"?: (nextValue: T) => void\n /** Callback any time the control is clicked (even if a new value was not selected) */\n \"onClick\"?: () => void\n /**\n * Text read aloud to screen readers when the control is focused\n */\n \"aria-label\": string\n /**\n * Visual variant of the tab group\n * - `\"segmented\"` — background container with sliding highlight (default)\n * - `\"underline\"` — no background, animated line indicator under active tab\n * @default \"segmented\"\n */\n \"variant\"?: TabsVariant\n /**\n * Orientation of the tab layout\n * @default \"horizontal\"\n */\n \"orientation\"?: TabsOrientation\n /**\n * Controls the size of the tabs\n *\n * | 3xs | 2xs | xs | sm | md | lg | xl | 2xl | 3xl |\n * | ------- | ------- | ------- | ------- | ------- | ------- | ------- | ------- | ------- |\n * | `22px` | `24px` | `26px` | `28px` | `32px` | `36px` | `40px` | `44px` | `48px` |\n *\n * @default md\n */\n \"size\"?: ControlSize\n /**\n * Controls gutter on the edges of the button, defaults to value from `size`.\n *\n * | 2xs | xs | sm | md | lg | xl |\n * | ------ | ------ | ------ | ------ | ------ | ------ |\n * | `6px` | `8px` | `10px` | `12px` | `14px` | `16px` |\n */\n \"gutterSize\"?: Sizes<\"2xs\" | \"xs\" | \"sm\" | \"md\" | \"lg\" | \"xl\">\n /** Disable the entire group */\n \"disabled\"?: boolean\n /**\n * Display the control as a block element with equal width segments\n * @default false\n */\n \"block\"?: boolean\n /**\n * Determines if the tabs should be a fully rounded pill shape.\n * Only applies to the `\"segmented\"` variant.\n * @default true\n */\n \"pill\"?: boolean\n \"className\"?: string\n \"children\": React.ReactNode\n}\n\nexport const Tabs = <T extends string>({\n value,\n onChange,\n children,\n variant = \"segmented\",\n orientation = \"horizontal\",\n block,\n pill = true,\n size = \"md\",\n gutterSize,\n className,\n onClick,\n ...restProps\n}: TabsProps<T>) => {\n const rootRef = useRef<HTMLDivElement>(null)\n const thumbRef = useRef<HTMLDivElement>(null)\n const prevSizeRef = useRef(size)\n const isVertical = orientation === \"vertical\"\n\n const applyThumbSizing = useCallback(\n (attemptScroll: boolean) => {\n const root = rootRef.current\n const thumb = thumbRef.current\n\n if (!root || !thumb) {\n return\n }\n\n // Get selected node\n const activeNode = root?.querySelector<HTMLDivElement>('[data-state=\"on\"]')\n\n // Impossible\n if (!activeNode) {\n return\n }\n\n if (isVertical) {\n const rootHeight = root.clientHeight\n let targetHeight = Math.floor(activeNode.clientHeight)\n const targetOffset = activeNode.offsetTop\n\n // Detect subpixel edge case\n if (rootHeight - (targetHeight + targetOffset) < 2) {\n targetHeight = targetHeight - 1\n }\n\n thumb.style.height = `${Math.floor(targetHeight)}px`\n thumb.style.width = \"\"\n thumb.style.transform = `translateY(${targetOffset}px)`\n\n // Scroll into view if needed\n if (root.scrollHeight > rootHeight) {\n const buffer = rootHeight * 0.15\n const scrollTop = root.scrollTop\n const top = activeNode.offsetTop\n const bottom = top + targetHeight\n if (top < scrollTop + buffer || bottom > scrollTop + rootHeight - buffer) {\n if (attemptScroll) {\n activeNode.scrollIntoView({ block: \"center\", inline: \"nearest\", behavior: \"smooth\" })\n }\n }\n }\n } else {\n const rootWidth = root.clientWidth\n let targetWidth = Math.floor(activeNode.clientWidth)\n const targetOffset = activeNode.offsetLeft\n\n // Detect if the thumb is moving too far to the edge of the container.\n // This would most commonly be due to subpixel widths adding up to excessive distance.\n if (rootWidth - (targetWidth + targetOffset) < 2) {\n targetWidth = targetWidth - 1\n }\n\n thumb.style.width = `${Math.floor(targetWidth)}px`\n thumb.style.height = \"\"\n thumb.style.transform = `translateX(${targetOffset}px)`\n\n // If the control is scrollable, ensure the active option is visible\n if (root.scrollWidth > rootWidth) {\n // Only scroll items near the edge, but not the inner 2/3.\n const buffer = rootWidth * 0.15\n const scrollLeft = root.scrollLeft\n const left = activeNode.offsetLeft\n const right = left + targetWidth\n if (left < scrollLeft + buffer || right > scrollLeft + rootWidth - buffer) {\n // Cheap trick to avoid unintentional scroll on mount - transition is set after mounting\n if (attemptScroll) {\n activeNode.scrollIntoView({ block: \"nearest\", inline: \"center\", behavior: \"smooth\" })\n }\n }\n }\n }\n },\n [isVertical],\n )\n\n useResizeObserver({\n // @ts-expect-error(2322) -- bug in types: https://github.com/juliencrn/usehooks-ts/issues/663\n ref: rootRef,\n onResize: () => {\n const thumb = thumbRef.current\n\n if (!thumb) {\n return\n }\n\n // Perform the size update instantly\n const currentTransition = thumb.style.transition\n thumb.style.transition = \"\"\n applyThumbSizing(false)\n thumb.style.transition = currentTransition\n },\n })\n\n const transitionProperty = isVertical\n ? \"height 300ms var(--cubic-enter), transform 300ms var(--cubic-enter)\"\n : \"width 300ms var(--cubic-enter), transform 300ms var(--cubic-enter)\"\n\n useLayoutEffect(() => {\n const root = rootRef.current\n const thumb = thumbRef.current\n\n if (!root || !thumb) {\n return\n }\n\n const sizeChanged = prevSizeRef.current !== size\n prevSizeRef.current = size\n\n if (sizeChanged) {\n // Size changed - disable transition, wait for CSS, then apply sizing\n const currentTransition = thumb.style.transition\n thumb.style.transition = \"\"\n\n waitForAnimationFrame(() => {\n applyThumbSizing(false)\n waitForAnimationFrame(() => {\n thumb.style.transition = currentTransition\n })\n })\n } else {\n // Normal update (value change, etc.)\n waitForAnimationFrame(() => {\n applyThumbSizing(!!thumb.style.transition)\n\n // Apply transition after initial calculation is set\n if (!thumb.style.transition) {\n waitForAnimationFrame(() => {\n thumb.style.transition = transitionProperty\n })\n }\n })\n }\n }, [applyThumbSizing, value, size, gutterSize, pill, transitionProperty])\n\n const handleValueChange = (nextValue: T) => {\n // Only trigger onChange when a value exists\n // Disallow toggling off enabled items\n if (nextValue && onChange) onChange(nextValue)\n }\n\n // Only apply pill for segmented variant\n const isPill = variant === \"segmented\" && pill\n\n return (\n <ToggleGroup.Root\n ref={rootRef}\n className={clsx(s.Tabs, className)}\n type=\"single\"\n value={value}\n loop={false}\n onValueChange={handleValueChange}\n onClick={onClick}\n data-variant={variant}\n data-orientation={orientation}\n data-block={block ? \"\" : undefined}\n data-pill={isPill ? \"\" : undefined}\n data-size={size}\n data-gutter-size={gutterSize}\n {...restProps}\n >\n <div className={s.TabsThumb} ref={thumbRef} />\n {children}\n </ToggleGroup.Root>\n )\n}\n\n/**\n * Badge configuration for Tabs.Tab\n */\nexport type TabsBadgeProp =\n | React.ReactNode\n | {\n content: React.ReactNode\n color?: SemanticColors<\n \"secondary\" | \"success\" | \"danger\" | \"warning\" | \"info\" | \"discovery\" | \"caution\"\n >\n variant?: Variants<\"soft\" | \"solid\">\n pill?: boolean\n loading?: boolean\n }\n\nexport type TabProps = {\n /**\n * Tab value\n */\n \"value\": string\n /**\n * Text read aloud to screen readers when the tab is focused\n */\n \"aria-label\"?: string\n /**\n * Text content to render in the tab\n */\n \"children\"?: React.ReactNode\n /**\n * Icon to render before the text content\n */\n \"icon\"?: React.ReactNode\n /**\n * Badge to render after the text content.\n * Can be a simple value or an object with content, color, variant, and loading options.\n * @example badge={5}\n * @example badge={{ content: 5, color: \"danger\" }}\n */\n \"badge\"?: TabsBadgeProp\n /**\n * Disable the individual tab\n */\n \"disabled\"?: boolean\n}\n\n// Type guard for badge object form\nconst isBadgeObject = (\n badge: TabsBadgeProp,\n): badge is Exclude<TabsBadgeProp, React.ReactNode> & { content: React.ReactNode } => {\n return badge != null && typeof badge === \"object\" && \"content\" in badge\n}\n\nconst Tab = ({ children, icon, badge, ...restProps }: TabProps) => {\n // Normalize badge prop\n const badgeProps = badge != null ? (isBadgeObject(badge) ? badge : { content: badge }) : null\n\n return (\n <ToggleGroup.Item\n className={s.Tab}\n {...restProps}\n onPointerEnter={handlePressableMouseEnter}\n >\n <span className={s.TabContent}>\n {icon}\n {children && <span>{children}</span>}\n {badgeProps && (\n <span\n className={s.TabBadge}\n data-color={badgeProps.color ?? \"secondary\"}\n data-variant={badgeProps.variant ?? \"soft\"}\n data-pill={badgeProps.pill ? \"\" : undefined}\n >\n {badgeProps.loading ? <LoadingIndicator /> : badgeProps.content}\n </span>\n )}\n </span>\n </ToggleGroup.Item>\n )\n}\n\n// Attach sub-components\nTabs.Tab = Tab\n// Backward-compat alias\nTabs.Option = Tab\n"]}
|