@telia/teddy 0.0.73 → 0.0.74
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.
|
@@ -64,76 +64,77 @@ const Corner = React.forwardRef(
|
|
|
64
64
|
}
|
|
65
65
|
);
|
|
66
66
|
Corner.displayName = "Corner";
|
|
67
|
+
const directionToAriaLabel = {
|
|
68
|
+
up: "Scroll opp",
|
|
69
|
+
down: "Scroll ned",
|
|
70
|
+
left: "Scroll til venstre",
|
|
71
|
+
right: "Scroll til høyre"
|
|
72
|
+
};
|
|
73
|
+
const directionToIconName = {
|
|
74
|
+
up: "chevron-up",
|
|
75
|
+
down: "chevron-down",
|
|
76
|
+
left: "chevron-left",
|
|
77
|
+
right: "chevron-right"
|
|
78
|
+
};
|
|
67
79
|
const Button = React.forwardRef(
|
|
68
80
|
({ className, hidden, direction = "right", children, ...props }, forwardRef) => {
|
|
69
|
-
const
|
|
70
|
-
const
|
|
71
|
-
const
|
|
81
|
+
const scrollAreaContext = React.useContext(RootContext);
|
|
82
|
+
const shouldBeHidden = hidden ?? (direction === "up" && !(scrollAreaContext == null ? void 0 : scrollAreaContext.hasScrollTop) || direction === "down" && !(scrollAreaContext == null ? void 0 : scrollAreaContext.hasScrollBottom) || direction === "left" && !(scrollAreaContext == null ? void 0 : scrollAreaContext.hasScrollLeft) || direction === "right" && !(scrollAreaContext == null ? void 0 : scrollAreaContext.hasScrollRight));
|
|
83
|
+
const buttonClasses = clsx(
|
|
72
84
|
styles[`${rootClassName}__button`],
|
|
73
85
|
{
|
|
74
|
-
[styles[`${rootClassName}__button--hidden`]]:
|
|
86
|
+
[styles[`${rootClassName}__button--hidden`]]: shouldBeHidden
|
|
75
87
|
},
|
|
76
88
|
className
|
|
77
89
|
);
|
|
78
|
-
const
|
|
79
|
-
|
|
80
|
-
down: "Scroll ned",
|
|
81
|
-
left: "Scroll til venstre",
|
|
82
|
-
right: "Scroll til høyre"
|
|
83
|
-
};
|
|
84
|
-
const directionIcons = {
|
|
85
|
-
up: "chevron-up",
|
|
86
|
-
down: "chevron-down",
|
|
87
|
-
left: "chevron-left",
|
|
88
|
-
right: "chevron-right"
|
|
89
|
-
};
|
|
90
|
-
const handleClick = utils_composeEventHandlers.composeEventHandlers(props.onClick, (e) => {
|
|
91
|
-
if (e.defaultPrevented)
|
|
90
|
+
const handleButtonClick = utils_composeEventHandlers.composeEventHandlers(props.onClick, (event) => {
|
|
91
|
+
if (event.defaultPrevented)
|
|
92
92
|
return;
|
|
93
|
-
const
|
|
94
|
-
if (!
|
|
93
|
+
const scrollAreaElement = scrollAreaContext == null ? void 0 : scrollAreaContext.scrollRef;
|
|
94
|
+
if (!scrollAreaElement)
|
|
95
95
|
return;
|
|
96
|
-
const
|
|
97
|
-
const
|
|
98
|
-
const
|
|
99
|
-
const
|
|
100
|
-
const
|
|
101
|
-
const
|
|
102
|
-
const
|
|
103
|
-
const
|
|
104
|
-
const
|
|
105
|
-
const
|
|
106
|
-
|
|
107
|
-
const
|
|
108
|
-
|
|
109
|
-
return { elementStart, elementEnd };
|
|
96
|
+
const scrollableItems = Array.from(scrollAreaElement.querySelectorAll("[data-in-view]"));
|
|
97
|
+
const isHorizontalScroll = direction === "left" || direction === "right";
|
|
98
|
+
const scrollProperty = isHorizontalScroll ? "left" : "top";
|
|
99
|
+
const isScrollingBackward = direction === "left" || direction === "up";
|
|
100
|
+
const currentScrollPosition = isHorizontalScroll ? scrollAreaElement.scrollLeft : scrollAreaElement.scrollTop;
|
|
101
|
+
const viewportSize = isHorizontalScroll ? scrollAreaElement.clientWidth : scrollAreaElement.clientHeight;
|
|
102
|
+
const scrollButtonSize = isHorizontalScroll ? event.currentTarget.offsetWidth : event.currentTarget.offsetHeight;
|
|
103
|
+
const visibleAreaStart = currentScrollPosition + (isScrollingBackward ? scrollButtonSize : 0);
|
|
104
|
+
const visibleAreaEnd = currentScrollPosition + viewportSize - (!isScrollingBackward ? scrollButtonSize : 0);
|
|
105
|
+
const getItemBounds = (item) => {
|
|
106
|
+
const itemStart = isHorizontalScroll ? item.offsetLeft : item.offsetTop;
|
|
107
|
+
const itemEnd = itemStart + (isHorizontalScroll ? item.offsetWidth : item.offsetHeight);
|
|
108
|
+
return { itemStart, itemEnd };
|
|
110
109
|
};
|
|
111
|
-
const
|
|
112
|
-
const {
|
|
113
|
-
return
|
|
110
|
+
const isItemFullyVisible = (item) => {
|
|
111
|
+
const { itemStart, itemEnd } = getItemBounds(item);
|
|
112
|
+
return itemStart >= visibleAreaStart && itemEnd <= visibleAreaEnd;
|
|
114
113
|
};
|
|
115
|
-
const
|
|
116
|
-
const
|
|
117
|
-
const
|
|
118
|
-
return
|
|
114
|
+
const fullyVisibleItemIndices = scrollableItems.map((item, index) => {
|
|
115
|
+
const isInView = item.getAttribute("data-in-view") === "true";
|
|
116
|
+
const isFullyVisible = isItemFullyVisible(item);
|
|
117
|
+
return isInView && isFullyVisible ? index : -1;
|
|
119
118
|
}).filter((index) => index !== -1);
|
|
120
|
-
const
|
|
121
|
-
const scrollBehavior =
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
const
|
|
128
|
-
const
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
const
|
|
132
|
-
|
|
119
|
+
const userPrefersReducedMotion = window.matchMedia("(prefers-reduced-motion: reduce)").matches;
|
|
120
|
+
const scrollBehavior = userPrefersReducedMotion ? "auto" : (scrollAreaContext == null ? void 0 : scrollAreaContext.scrollBehavior) ?? "smooth";
|
|
121
|
+
function calculateScrollAmount() {
|
|
122
|
+
if (fullyVisibleItemIndices.length <= 0) {
|
|
123
|
+
const defaultScrollAmount = viewportSize - scrollButtonSize * 2;
|
|
124
|
+
return isScrollingBackward ? currentScrollPosition - defaultScrollAmount : currentScrollPosition + defaultScrollAmount;
|
|
125
|
+
}
|
|
126
|
+
const isRightToLeft = scrollAreaContext == null ? void 0 : scrollAreaContext.isRtl;
|
|
127
|
+
const effectiveBackwardDirection = isRightToLeft ? !isScrollingBackward : isScrollingBackward;
|
|
128
|
+
const referenceItemIndex = effectiveBackwardDirection ? fullyVisibleItemIndices[0] : fullyVisibleItemIndices[fullyVisibleItemIndices.length - 1];
|
|
129
|
+
const targetItemIndex = effectiveBackwardDirection ? referenceItemIndex > 0 ? referenceItemIndex - 1 : referenceItemIndex : referenceItemIndex < scrollableItems.length - 1 ? referenceItemIndex + 1 : referenceItemIndex;
|
|
130
|
+
const targetItem = scrollableItems[targetItemIndex];
|
|
131
|
+
const { itemStart, itemEnd } = getItemBounds(targetItem);
|
|
132
|
+
const targetItemSize = itemEnd - itemStart;
|
|
133
|
+
return isScrollingBackward ? itemStart + targetItemSize - viewportSize + scrollButtonSize : itemStart - scrollButtonSize;
|
|
133
134
|
}
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
[
|
|
135
|
+
const newScrollPosition = calculateScrollAmount();
|
|
136
|
+
scrollAreaElement.scrollTo({
|
|
137
|
+
[scrollProperty]: newScrollPosition,
|
|
137
138
|
behavior: scrollBehavior
|
|
138
139
|
});
|
|
139
140
|
});
|
|
@@ -142,14 +143,14 @@ const Button = React.forwardRef(
|
|
|
142
143
|
{
|
|
143
144
|
iconOnly: true,
|
|
144
145
|
variant: "primary",
|
|
145
|
-
"aria-label":
|
|
146
|
+
"aria-label": directionToAriaLabel[direction],
|
|
146
147
|
"data-direction": direction,
|
|
147
|
-
tabIndex:
|
|
148
|
+
tabIndex: shouldBeHidden ? -1 : void 0,
|
|
148
149
|
...props,
|
|
149
|
-
onClick:
|
|
150
|
+
onClick: handleButtonClick,
|
|
150
151
|
ref: forwardRef,
|
|
151
|
-
className:
|
|
152
|
-
children: children || /* @__PURE__ */ jsxRuntime.jsx(components_icon_icon.Icon, { name:
|
|
152
|
+
className: buttonClasses,
|
|
153
|
+
children: children || /* @__PURE__ */ jsxRuntime.jsx(components_icon_icon.Icon, { name: directionToIconName[direction] })
|
|
153
154
|
}
|
|
154
155
|
);
|
|
155
156
|
}
|
|
@@ -206,7 +207,16 @@ const Root = React.forwardRef(
|
|
|
206
207
|
return /* @__PURE__ */ jsxRuntime.jsx(
|
|
207
208
|
RootContext.Provider,
|
|
208
209
|
{
|
|
209
|
-
value: {
|
|
210
|
+
value: {
|
|
211
|
+
scrollRef,
|
|
212
|
+
variant,
|
|
213
|
+
hasScrollTop,
|
|
214
|
+
hasScrollBottom,
|
|
215
|
+
hasScrollLeft,
|
|
216
|
+
hasScrollRight,
|
|
217
|
+
scrollBehavior,
|
|
218
|
+
isRtl: props.dir === "rtl"
|
|
219
|
+
},
|
|
210
220
|
children: /* @__PURE__ */ jsxRuntime.jsxs(ScrollAreaPrimitive__namespace.Root, { ...props, ref: forwardRef, className: classes, children: [
|
|
211
221
|
/* @__PURE__ */ jsxRuntime.jsx(
|
|
212
222
|
ScrollAreaPrimitive__namespace.Viewport,
|
|
@@ -45,76 +45,77 @@ const Corner = React__default.forwardRef(
|
|
|
45
45
|
}
|
|
46
46
|
);
|
|
47
47
|
Corner.displayName = "Corner";
|
|
48
|
+
const directionToAriaLabel = {
|
|
49
|
+
up: "Scroll opp",
|
|
50
|
+
down: "Scroll ned",
|
|
51
|
+
left: "Scroll til venstre",
|
|
52
|
+
right: "Scroll til høyre"
|
|
53
|
+
};
|
|
54
|
+
const directionToIconName = {
|
|
55
|
+
up: "chevron-up",
|
|
56
|
+
down: "chevron-down",
|
|
57
|
+
left: "chevron-left",
|
|
58
|
+
right: "chevron-right"
|
|
59
|
+
};
|
|
48
60
|
const Button = React__default.forwardRef(
|
|
49
61
|
({ className, hidden, direction = "right", children, ...props }, forwardRef) => {
|
|
50
|
-
const
|
|
51
|
-
const
|
|
52
|
-
const
|
|
62
|
+
const scrollAreaContext = React__default.useContext(RootContext);
|
|
63
|
+
const shouldBeHidden = hidden ?? (direction === "up" && !(scrollAreaContext == null ? void 0 : scrollAreaContext.hasScrollTop) || direction === "down" && !(scrollAreaContext == null ? void 0 : scrollAreaContext.hasScrollBottom) || direction === "left" && !(scrollAreaContext == null ? void 0 : scrollAreaContext.hasScrollLeft) || direction === "right" && !(scrollAreaContext == null ? void 0 : scrollAreaContext.hasScrollRight));
|
|
64
|
+
const buttonClasses = clsx(
|
|
53
65
|
styles[`${rootClassName}__button`],
|
|
54
66
|
{
|
|
55
|
-
[styles[`${rootClassName}__button--hidden`]]:
|
|
67
|
+
[styles[`${rootClassName}__button--hidden`]]: shouldBeHidden
|
|
56
68
|
},
|
|
57
69
|
className
|
|
58
70
|
);
|
|
59
|
-
const
|
|
60
|
-
|
|
61
|
-
down: "Scroll ned",
|
|
62
|
-
left: "Scroll til venstre",
|
|
63
|
-
right: "Scroll til høyre"
|
|
64
|
-
};
|
|
65
|
-
const directionIcons = {
|
|
66
|
-
up: "chevron-up",
|
|
67
|
-
down: "chevron-down",
|
|
68
|
-
left: "chevron-left",
|
|
69
|
-
right: "chevron-right"
|
|
70
|
-
};
|
|
71
|
-
const handleClick = composeEventHandlers(props.onClick, (e) => {
|
|
72
|
-
if (e.defaultPrevented)
|
|
71
|
+
const handleButtonClick = composeEventHandlers(props.onClick, (event) => {
|
|
72
|
+
if (event.defaultPrevented)
|
|
73
73
|
return;
|
|
74
|
-
const
|
|
75
|
-
if (!
|
|
74
|
+
const scrollAreaElement = scrollAreaContext == null ? void 0 : scrollAreaContext.scrollRef;
|
|
75
|
+
if (!scrollAreaElement)
|
|
76
76
|
return;
|
|
77
|
-
const
|
|
78
|
-
const
|
|
79
|
-
const
|
|
80
|
-
const
|
|
81
|
-
const
|
|
82
|
-
const
|
|
83
|
-
const
|
|
84
|
-
const
|
|
85
|
-
const
|
|
86
|
-
const
|
|
87
|
-
|
|
88
|
-
const
|
|
89
|
-
|
|
90
|
-
return { elementStart, elementEnd };
|
|
77
|
+
const scrollableItems = Array.from(scrollAreaElement.querySelectorAll("[data-in-view]"));
|
|
78
|
+
const isHorizontalScroll = direction === "left" || direction === "right";
|
|
79
|
+
const scrollProperty = isHorizontalScroll ? "left" : "top";
|
|
80
|
+
const isScrollingBackward = direction === "left" || direction === "up";
|
|
81
|
+
const currentScrollPosition = isHorizontalScroll ? scrollAreaElement.scrollLeft : scrollAreaElement.scrollTop;
|
|
82
|
+
const viewportSize = isHorizontalScroll ? scrollAreaElement.clientWidth : scrollAreaElement.clientHeight;
|
|
83
|
+
const scrollButtonSize = isHorizontalScroll ? event.currentTarget.offsetWidth : event.currentTarget.offsetHeight;
|
|
84
|
+
const visibleAreaStart = currentScrollPosition + (isScrollingBackward ? scrollButtonSize : 0);
|
|
85
|
+
const visibleAreaEnd = currentScrollPosition + viewportSize - (!isScrollingBackward ? scrollButtonSize : 0);
|
|
86
|
+
const getItemBounds = (item) => {
|
|
87
|
+
const itemStart = isHorizontalScroll ? item.offsetLeft : item.offsetTop;
|
|
88
|
+
const itemEnd = itemStart + (isHorizontalScroll ? item.offsetWidth : item.offsetHeight);
|
|
89
|
+
return { itemStart, itemEnd };
|
|
91
90
|
};
|
|
92
|
-
const
|
|
93
|
-
const {
|
|
94
|
-
return
|
|
91
|
+
const isItemFullyVisible = (item) => {
|
|
92
|
+
const { itemStart, itemEnd } = getItemBounds(item);
|
|
93
|
+
return itemStart >= visibleAreaStart && itemEnd <= visibleAreaEnd;
|
|
95
94
|
};
|
|
96
|
-
const
|
|
97
|
-
const
|
|
98
|
-
const
|
|
99
|
-
return
|
|
95
|
+
const fullyVisibleItemIndices = scrollableItems.map((item, index) => {
|
|
96
|
+
const isInView = item.getAttribute("data-in-view") === "true";
|
|
97
|
+
const isFullyVisible = isItemFullyVisible(item);
|
|
98
|
+
return isInView && isFullyVisible ? index : -1;
|
|
100
99
|
}).filter((index) => index !== -1);
|
|
101
|
-
const
|
|
102
|
-
const scrollBehavior =
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
const
|
|
109
|
-
const
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
const
|
|
113
|
-
|
|
100
|
+
const userPrefersReducedMotion = window.matchMedia("(prefers-reduced-motion: reduce)").matches;
|
|
101
|
+
const scrollBehavior = userPrefersReducedMotion ? "auto" : (scrollAreaContext == null ? void 0 : scrollAreaContext.scrollBehavior) ?? "smooth";
|
|
102
|
+
function calculateScrollAmount() {
|
|
103
|
+
if (fullyVisibleItemIndices.length <= 0) {
|
|
104
|
+
const defaultScrollAmount = viewportSize - scrollButtonSize * 2;
|
|
105
|
+
return isScrollingBackward ? currentScrollPosition - defaultScrollAmount : currentScrollPosition + defaultScrollAmount;
|
|
106
|
+
}
|
|
107
|
+
const isRightToLeft = scrollAreaContext == null ? void 0 : scrollAreaContext.isRtl;
|
|
108
|
+
const effectiveBackwardDirection = isRightToLeft ? !isScrollingBackward : isScrollingBackward;
|
|
109
|
+
const referenceItemIndex = effectiveBackwardDirection ? fullyVisibleItemIndices[0] : fullyVisibleItemIndices[fullyVisibleItemIndices.length - 1];
|
|
110
|
+
const targetItemIndex = effectiveBackwardDirection ? referenceItemIndex > 0 ? referenceItemIndex - 1 : referenceItemIndex : referenceItemIndex < scrollableItems.length - 1 ? referenceItemIndex + 1 : referenceItemIndex;
|
|
111
|
+
const targetItem = scrollableItems[targetItemIndex];
|
|
112
|
+
const { itemStart, itemEnd } = getItemBounds(targetItem);
|
|
113
|
+
const targetItemSize = itemEnd - itemStart;
|
|
114
|
+
return isScrollingBackward ? itemStart + targetItemSize - viewportSize + scrollButtonSize : itemStart - scrollButtonSize;
|
|
114
115
|
}
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
[
|
|
116
|
+
const newScrollPosition = calculateScrollAmount();
|
|
117
|
+
scrollAreaElement.scrollTo({
|
|
118
|
+
[scrollProperty]: newScrollPosition,
|
|
118
119
|
behavior: scrollBehavior
|
|
119
120
|
});
|
|
120
121
|
});
|
|
@@ -123,14 +124,14 @@ const Button = React__default.forwardRef(
|
|
|
123
124
|
{
|
|
124
125
|
iconOnly: true,
|
|
125
126
|
variant: "primary",
|
|
126
|
-
"aria-label":
|
|
127
|
+
"aria-label": directionToAriaLabel[direction],
|
|
127
128
|
"data-direction": direction,
|
|
128
|
-
tabIndex:
|
|
129
|
+
tabIndex: shouldBeHidden ? -1 : void 0,
|
|
129
130
|
...props,
|
|
130
|
-
onClick:
|
|
131
|
+
onClick: handleButtonClick,
|
|
131
132
|
ref: forwardRef,
|
|
132
|
-
className:
|
|
133
|
-
children: children || /* @__PURE__ */ jsx(Icon, { name:
|
|
133
|
+
className: buttonClasses,
|
|
134
|
+
children: children || /* @__PURE__ */ jsx(Icon, { name: directionToIconName[direction] })
|
|
134
135
|
}
|
|
135
136
|
);
|
|
136
137
|
}
|
|
@@ -187,7 +188,16 @@ const Root = React__default.forwardRef(
|
|
|
187
188
|
return /* @__PURE__ */ jsx(
|
|
188
189
|
RootContext.Provider,
|
|
189
190
|
{
|
|
190
|
-
value: {
|
|
191
|
+
value: {
|
|
192
|
+
scrollRef,
|
|
193
|
+
variant,
|
|
194
|
+
hasScrollTop,
|
|
195
|
+
hasScrollBottom,
|
|
196
|
+
hasScrollLeft,
|
|
197
|
+
hasScrollRight,
|
|
198
|
+
scrollBehavior,
|
|
199
|
+
isRtl: props.dir === "rtl"
|
|
200
|
+
},
|
|
191
201
|
children: /* @__PURE__ */ jsxs(ScrollAreaPrimitive.Root, { ...props, ref: forwardRef, className: classes, children: [
|
|
192
202
|
/* @__PURE__ */ jsx(
|
|
193
203
|
ScrollAreaPrimitive.Viewport,
|
|
@@ -15,7 +15,7 @@ export declare const Button: React.ForwardRefExoticComponent<Omit<Omit<Omit<Reac
|
|
|
15
15
|
} & {
|
|
16
16
|
asChild?: boolean | undefined;
|
|
17
17
|
loading?: boolean | undefined;
|
|
18
|
-
fullWidth?: boolean | undefined;
|
|
18
|
+
fullWidth?: boolean | undefined; /** Hidden will fade the button out. Used when the scrollbar is at the end */
|
|
19
19
|
size?: "sm" | "md" | "lg" | undefined;
|
|
20
20
|
variant?: "text" | "text-negative" | "primary" | "list-item" | "secondary" | "destructive" | "tertiary-purple" | "expressive" | "primary-negative" | "secondary-negative" | "destructive-negative" | "tertiary-purple-negative" | "expressive-negative" | undefined;
|
|
21
21
|
} & {
|
|
@@ -26,7 +26,7 @@ export declare const Button: React.ForwardRefExoticComponent<Omit<Omit<Omit<Reac
|
|
|
26
26
|
} & {
|
|
27
27
|
asChild?: boolean | undefined;
|
|
28
28
|
loading?: boolean | undefined;
|
|
29
|
-
fullWidth?: boolean | undefined;
|
|
29
|
+
fullWidth?: boolean | undefined; /** Hidden will fade the button out. Used when the scrollbar is at the end */
|
|
30
30
|
size?: "sm" | "md" | "lg" | undefined;
|
|
31
31
|
variant?: "text" | "text-negative" | "primary" | "list-item" | "secondary" | "destructive" | "tertiary-purple" | "expressive" | "primary-negative" | "secondary-negative" | "destructive-negative" | "tertiary-purple-negative" | "expressive-negative" | undefined;
|
|
32
32
|
} & {
|
|
@@ -20,6 +20,7 @@ type RootContextType = {
|
|
|
20
20
|
hasScrollLeft: boolean | undefined;
|
|
21
21
|
hasScrollRight: boolean | undefined;
|
|
22
22
|
scrollBehavior: 'smooth' | 'auto';
|
|
23
|
+
isRtl: boolean;
|
|
23
24
|
};
|
|
24
25
|
export declare const RootContext: React.Context<RootContextType | undefined>;
|
|
25
26
|
export declare const Root: React.ForwardRefExoticComponent<Omit<ScrollAreaPrimitive.ScrollAreaProps & React.RefAttributes<HTMLDivElement>, "ref"> & {
|