@trackunit/react-components 7.0.2 → 7.0.3
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/index.cjs.js
CHANGED
|
@@ -1553,7 +1553,6 @@ const cvaButton = cssClassVarianceUtilities.cva({
|
|
|
1553
1553
|
"disabled:cursor-not-allowed",
|
|
1554
1554
|
"font-medium",
|
|
1555
1555
|
"align-middle",
|
|
1556
|
-
"rounded-md",
|
|
1557
1556
|
],
|
|
1558
1557
|
variants: {
|
|
1559
1558
|
variant: {
|
|
@@ -12734,6 +12733,50 @@ const cvaToggleItemContent = cssClassVarianceUtilities.cva({
|
|
|
12734
12733
|
],
|
|
12735
12734
|
});
|
|
12736
12735
|
|
|
12736
|
+
/**
|
|
12737
|
+
* Measures the sliding indicator of a ToggleGroup, the left offset and width of the selected item, and keeps it in
|
|
12738
|
+
* sync as the items resize.
|
|
12739
|
+
*
|
|
12740
|
+
* Every item is observed, not only the selected one. The left offset depends on all the items before the selected one,
|
|
12741
|
+
* and items change width after mount, for example when a label carries data that loads asynchronously.
|
|
12742
|
+
*
|
|
12743
|
+
* `useMeasure` is not used because it observes a single element. Observing only the container would miss item resizes
|
|
12744
|
+
* when a consumer fixes the container width through `className` or `style` (for example `w-full`). `useMeasure` also
|
|
12745
|
+
* reports `getBoundingClientRect()`, which includes CSS transforms, where the indicator needs the layout values
|
|
12746
|
+
* `offsetLeft` and `offsetWidth`.
|
|
12747
|
+
*
|
|
12748
|
+
* Measured in `useLayoutEffect`, so a re-measure lands before the browser paints instead of painting a stale
|
|
12749
|
+
* indicator for a frame.
|
|
12750
|
+
*
|
|
12751
|
+
* The observer is disconnected and rebuilt over all buttons whenever `selectedIndex` or `itemIds` changes. That is
|
|
12752
|
+
* cheap for the short lists a ToggleGroup is meant for. Lists with many options belong in a Select or RadioGroup.
|
|
12753
|
+
*
|
|
12754
|
+
* @param {UseSlidingIndicatorProps} props - The item buttons, the selected index and the identity of the items.
|
|
12755
|
+
* @returns {SlidingIndicatorPosition} The `left` and `width` of the indicator in px.
|
|
12756
|
+
*/
|
|
12757
|
+
const useSlidingIndicator = (props) => {
|
|
12758
|
+
const { buttonRefs, selectedIndex, itemIds } = props;
|
|
12759
|
+
const [position, setPosition] = react.useState({ left: 0, width: 0 });
|
|
12760
|
+
react.useLayoutEffect(() => {
|
|
12761
|
+
const measure = () => {
|
|
12762
|
+
const selectedButton = buttonRefs.current[selectedIndex];
|
|
12763
|
+
const left = selectedButton?.offsetLeft ?? 0;
|
|
12764
|
+
const width = selectedButton?.offsetWidth ?? 0;
|
|
12765
|
+
// Keep the same object when nothing moved, so a notification that changes nothing does not render again.
|
|
12766
|
+
setPosition(previous => (previous.left === left && previous.width === width ? previous : { left, width }));
|
|
12767
|
+
};
|
|
12768
|
+
measure();
|
|
12769
|
+
const resizeObserver = new ResizeObserver(measure);
|
|
12770
|
+
buttonRefs.current.forEach(button => {
|
|
12771
|
+
if (button) {
|
|
12772
|
+
resizeObserver.observe(button);
|
|
12773
|
+
}
|
|
12774
|
+
});
|
|
12775
|
+
return () => resizeObserver.disconnect();
|
|
12776
|
+
}, [buttonRefs, selectedIndex, itemIds]);
|
|
12777
|
+
return position;
|
|
12778
|
+
};
|
|
12779
|
+
|
|
12737
12780
|
/**
|
|
12738
12781
|
* ToggleGroup allows users to toggle between two or more closely related options and immediately apply the selection.
|
|
12739
12782
|
* It renders a segmented control with a sliding background indicator. Supports text-only, icon-only, and text+icon modes.
|
|
@@ -12772,8 +12815,6 @@ const cvaToggleItemContent = cssClassVarianceUtilities.cva({
|
|
|
12772
12815
|
const ToggleGroup = (props) => {
|
|
12773
12816
|
const { selected, setSelected, onChange, disabled = false, size = "medium", className, "data-testid": dataTestId, ref, style, } = props;
|
|
12774
12817
|
const [isMounted, setIsMounted] = react.useState(false);
|
|
12775
|
-
const [slidingLeft, setSlidingLeft] = react.useState(0);
|
|
12776
|
-
const [slidingWidth, setSlidingWidth] = react.useState(0);
|
|
12777
12818
|
const buttonRefs = react.useRef([]);
|
|
12778
12819
|
const selectedIndex = props.list.findIndex(item => item.id === selected);
|
|
12779
12820
|
const validIndex = selectedIndex >= 0 ? selectedIndex : 0;
|
|
@@ -12782,15 +12823,11 @@ const ToggleGroup = (props) => {
|
|
|
12782
12823
|
setIsMounted(true);
|
|
12783
12824
|
});
|
|
12784
12825
|
}, []);
|
|
12785
|
-
|
|
12786
|
-
|
|
12787
|
-
|
|
12788
|
-
|
|
12789
|
-
|
|
12790
|
-
const width = buttonRefs.current[validIndex]?.offsetWidth ?? 0;
|
|
12791
|
-
setSlidingLeft(left);
|
|
12792
|
-
setSlidingWidth(width);
|
|
12793
|
-
}, [validIndex]);
|
|
12826
|
+
// Joined into one string so the indicator is measured again when items are added, removed or reordered, but not on
|
|
12827
|
+
// every render, which would see a new array each time. A null character cannot be part of an id, so ids cannot run
|
|
12828
|
+
// together into a false match.
|
|
12829
|
+
const itemIds = props.list.map(item => item.id).join("\u0000");
|
|
12830
|
+
const slidingIndicator = useSlidingIndicator({ buttonRefs, selectedIndex: validIndex, itemIds });
|
|
12794
12831
|
const buildCommonItemProps = (item, index) => ({
|
|
12795
12832
|
className: item.className,
|
|
12796
12833
|
"data-testid": item["data-testid"],
|
|
@@ -12809,8 +12846,8 @@ const ToggleGroup = (props) => {
|
|
|
12809
12846
|
});
|
|
12810
12847
|
// eslint-disable-next-line @trackunit/no-typescript-assertion
|
|
12811
12848
|
const slidingStyle = {
|
|
12812
|
-
"--sliding-left": `${
|
|
12813
|
-
"--sliding-width": `${
|
|
12849
|
+
"--sliding-left": `${slidingIndicator.left}px`,
|
|
12850
|
+
"--sliding-width": `${slidingIndicator.width}px`,
|
|
12814
12851
|
"--transition-duration": "200ms",
|
|
12815
12852
|
};
|
|
12816
12853
|
return (jsxRuntime.jsx("div", { className: tailwindMerge.twMerge(cvaToggleGroup({ className }), cvaToggleGroupWithSlidingBackground({ isMounted })), "data-testid": dataTestId, ref: ref, style: mergeDomStyles(slidingStyle, style), children: props.isIconOnly
|
package/index.esm.js
CHANGED
|
@@ -1552,7 +1552,6 @@ const cvaButton = cva({
|
|
|
1552
1552
|
"disabled:cursor-not-allowed",
|
|
1553
1553
|
"font-medium",
|
|
1554
1554
|
"align-middle",
|
|
1555
|
-
"rounded-md",
|
|
1556
1555
|
],
|
|
1557
1556
|
variants: {
|
|
1558
1557
|
variant: {
|
|
@@ -12733,6 +12732,50 @@ const cvaToggleItemContent = cva({
|
|
|
12733
12732
|
],
|
|
12734
12733
|
});
|
|
12735
12734
|
|
|
12735
|
+
/**
|
|
12736
|
+
* Measures the sliding indicator of a ToggleGroup, the left offset and width of the selected item, and keeps it in
|
|
12737
|
+
* sync as the items resize.
|
|
12738
|
+
*
|
|
12739
|
+
* Every item is observed, not only the selected one. The left offset depends on all the items before the selected one,
|
|
12740
|
+
* and items change width after mount, for example when a label carries data that loads asynchronously.
|
|
12741
|
+
*
|
|
12742
|
+
* `useMeasure` is not used because it observes a single element. Observing only the container would miss item resizes
|
|
12743
|
+
* when a consumer fixes the container width through `className` or `style` (for example `w-full`). `useMeasure` also
|
|
12744
|
+
* reports `getBoundingClientRect()`, which includes CSS transforms, where the indicator needs the layout values
|
|
12745
|
+
* `offsetLeft` and `offsetWidth`.
|
|
12746
|
+
*
|
|
12747
|
+
* Measured in `useLayoutEffect`, so a re-measure lands before the browser paints instead of painting a stale
|
|
12748
|
+
* indicator for a frame.
|
|
12749
|
+
*
|
|
12750
|
+
* The observer is disconnected and rebuilt over all buttons whenever `selectedIndex` or `itemIds` changes. That is
|
|
12751
|
+
* cheap for the short lists a ToggleGroup is meant for. Lists with many options belong in a Select or RadioGroup.
|
|
12752
|
+
*
|
|
12753
|
+
* @param {UseSlidingIndicatorProps} props - The item buttons, the selected index and the identity of the items.
|
|
12754
|
+
* @returns {SlidingIndicatorPosition} The `left` and `width` of the indicator in px.
|
|
12755
|
+
*/
|
|
12756
|
+
const useSlidingIndicator = (props) => {
|
|
12757
|
+
const { buttonRefs, selectedIndex, itemIds } = props;
|
|
12758
|
+
const [position, setPosition] = useState({ left: 0, width: 0 });
|
|
12759
|
+
useLayoutEffect(() => {
|
|
12760
|
+
const measure = () => {
|
|
12761
|
+
const selectedButton = buttonRefs.current[selectedIndex];
|
|
12762
|
+
const left = selectedButton?.offsetLeft ?? 0;
|
|
12763
|
+
const width = selectedButton?.offsetWidth ?? 0;
|
|
12764
|
+
// Keep the same object when nothing moved, so a notification that changes nothing does not render again.
|
|
12765
|
+
setPosition(previous => (previous.left === left && previous.width === width ? previous : { left, width }));
|
|
12766
|
+
};
|
|
12767
|
+
measure();
|
|
12768
|
+
const resizeObserver = new ResizeObserver(measure);
|
|
12769
|
+
buttonRefs.current.forEach(button => {
|
|
12770
|
+
if (button) {
|
|
12771
|
+
resizeObserver.observe(button);
|
|
12772
|
+
}
|
|
12773
|
+
});
|
|
12774
|
+
return () => resizeObserver.disconnect();
|
|
12775
|
+
}, [buttonRefs, selectedIndex, itemIds]);
|
|
12776
|
+
return position;
|
|
12777
|
+
};
|
|
12778
|
+
|
|
12736
12779
|
/**
|
|
12737
12780
|
* ToggleGroup allows users to toggle between two or more closely related options and immediately apply the selection.
|
|
12738
12781
|
* It renders a segmented control with a sliding background indicator. Supports text-only, icon-only, and text+icon modes.
|
|
@@ -12771,8 +12814,6 @@ const cvaToggleItemContent = cva({
|
|
|
12771
12814
|
const ToggleGroup = (props) => {
|
|
12772
12815
|
const { selected, setSelected, onChange, disabled = false, size = "medium", className, "data-testid": dataTestId, ref, style, } = props;
|
|
12773
12816
|
const [isMounted, setIsMounted] = useState(false);
|
|
12774
|
-
const [slidingLeft, setSlidingLeft] = useState(0);
|
|
12775
|
-
const [slidingWidth, setSlidingWidth] = useState(0);
|
|
12776
12817
|
const buttonRefs = useRef([]);
|
|
12777
12818
|
const selectedIndex = props.list.findIndex(item => item.id === selected);
|
|
12778
12819
|
const validIndex = selectedIndex >= 0 ? selectedIndex : 0;
|
|
@@ -12781,15 +12822,11 @@ const ToggleGroup = (props) => {
|
|
|
12781
12822
|
setIsMounted(true);
|
|
12782
12823
|
});
|
|
12783
12824
|
}, []);
|
|
12784
|
-
|
|
12785
|
-
|
|
12786
|
-
|
|
12787
|
-
|
|
12788
|
-
|
|
12789
|
-
const width = buttonRefs.current[validIndex]?.offsetWidth ?? 0;
|
|
12790
|
-
setSlidingLeft(left);
|
|
12791
|
-
setSlidingWidth(width);
|
|
12792
|
-
}, [validIndex]);
|
|
12825
|
+
// Joined into one string so the indicator is measured again when items are added, removed or reordered, but not on
|
|
12826
|
+
// every render, which would see a new array each time. A null character cannot be part of an id, so ids cannot run
|
|
12827
|
+
// together into a false match.
|
|
12828
|
+
const itemIds = props.list.map(item => item.id).join("\u0000");
|
|
12829
|
+
const slidingIndicator = useSlidingIndicator({ buttonRefs, selectedIndex: validIndex, itemIds });
|
|
12793
12830
|
const buildCommonItemProps = (item, index) => ({
|
|
12794
12831
|
className: item.className,
|
|
12795
12832
|
"data-testid": item["data-testid"],
|
|
@@ -12808,8 +12845,8 @@ const ToggleGroup = (props) => {
|
|
|
12808
12845
|
});
|
|
12809
12846
|
// eslint-disable-next-line @trackunit/no-typescript-assertion
|
|
12810
12847
|
const slidingStyle = {
|
|
12811
|
-
"--sliding-left": `${
|
|
12812
|
-
"--sliding-width": `${
|
|
12848
|
+
"--sliding-left": `${slidingIndicator.left}px`,
|
|
12849
|
+
"--sliding-width": `${slidingIndicator.width}px`,
|
|
12813
12850
|
"--transition-duration": "200ms",
|
|
12814
12851
|
};
|
|
12815
12852
|
return (jsx("div", { className: twMerge(cvaToggleGroup({ className }), cvaToggleGroupWithSlidingBackground({ isMounted })), "data-testid": dataTestId, ref: ref, style: mergeDomStyles(slidingStyle, style), children: props.isIconOnly
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@trackunit/react-router-components",
|
|
3
|
-
"version": "2.0.
|
|
3
|
+
"version": "2.0.3",
|
|
4
4
|
"repository": "https://github.com/Trackunit/manager",
|
|
5
5
|
"license": "SEE LICENSE IN LICENSE.txt",
|
|
6
6
|
"engines": {
|
|
@@ -9,8 +9,8 @@
|
|
|
9
9
|
"dependencies": {
|
|
10
10
|
"@trackunit/css-class-variance-utilities": "^2.6.57",
|
|
11
11
|
"@trackunit/i18n-library-translation": "^2.14.51",
|
|
12
|
-
"@trackunit/react-components": "^7.0.
|
|
13
|
-
"@trackunit/react-drawer": "^3.0.
|
|
12
|
+
"@trackunit/react-components": "^7.0.3",
|
|
13
|
+
"@trackunit/react-drawer": "^3.0.3",
|
|
14
14
|
"@trackunit/shared-utils": "^1.22.57",
|
|
15
15
|
"dequal": "^2.0.3",
|
|
16
16
|
"zod": "^3.25.76"
|
package/package.json
CHANGED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { RefObject } from "react";
|
|
2
|
+
/**
|
|
3
|
+
* Where the sliding indicator sits, in px, relative to the padding box of the ToggleGroup container.
|
|
4
|
+
*/
|
|
5
|
+
export type SlidingIndicatorPosition = {
|
|
6
|
+
readonly left: number;
|
|
7
|
+
readonly width: number;
|
|
8
|
+
};
|
|
9
|
+
export type UseSlidingIndicatorProps = {
|
|
10
|
+
/**
|
|
11
|
+
* The item buttons in list order. Items that are not rendered are skipped.
|
|
12
|
+
*/
|
|
13
|
+
buttonRefs: RefObject<Array<HTMLButtonElement | null>>;
|
|
14
|
+
/**
|
|
15
|
+
* Index of the selected item in `buttonRefs`.
|
|
16
|
+
*/
|
|
17
|
+
selectedIndex: number;
|
|
18
|
+
/**
|
|
19
|
+
* Identifies the current set of items. The buttons are measured and observed again when it changes, so it has to
|
|
20
|
+
* change when items are added, removed or reordered. It should not change on every render.
|
|
21
|
+
*/
|
|
22
|
+
itemIds: string;
|
|
23
|
+
};
|
|
24
|
+
/**
|
|
25
|
+
* Measures the sliding indicator of a ToggleGroup, the left offset and width of the selected item, and keeps it in
|
|
26
|
+
* sync as the items resize.
|
|
27
|
+
*
|
|
28
|
+
* Every item is observed, not only the selected one. The left offset depends on all the items before the selected one,
|
|
29
|
+
* and items change width after mount, for example when a label carries data that loads asynchronously.
|
|
30
|
+
*
|
|
31
|
+
* `useMeasure` is not used because it observes a single element. Observing only the container would miss item resizes
|
|
32
|
+
* when a consumer fixes the container width through `className` or `style` (for example `w-full`). `useMeasure` also
|
|
33
|
+
* reports `getBoundingClientRect()`, which includes CSS transforms, where the indicator needs the layout values
|
|
34
|
+
* `offsetLeft` and `offsetWidth`.
|
|
35
|
+
*
|
|
36
|
+
* Measured in `useLayoutEffect`, so a re-measure lands before the browser paints instead of painting a stale
|
|
37
|
+
* indicator for a frame.
|
|
38
|
+
*
|
|
39
|
+
* The observer is disconnected and rebuilt over all buttons whenever `selectedIndex` or `itemIds` changes. That is
|
|
40
|
+
* cheap for the short lists a ToggleGroup is meant for. Lists with many options belong in a Select or RadioGroup.
|
|
41
|
+
*
|
|
42
|
+
* @param {UseSlidingIndicatorProps} props - The item buttons, the selected index and the identity of the items.
|
|
43
|
+
* @returns {SlidingIndicatorPosition} The `left` and `width` of the indicator in px.
|
|
44
|
+
*/
|
|
45
|
+
export declare const useSlidingIndicator: (props: UseSlidingIndicatorProps) => SlidingIndicatorPosition;
|