@vygruppen/spor-react 12.24.15 → 12.24.16

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/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@vygruppen/spor-react",
3
3
  "type": "module",
4
- "version": "12.24.15",
4
+ "version": "12.24.16",
5
5
  "exports": {
6
6
  ".": {
7
7
  "types": "./dist/index.d.ts",
@@ -46,8 +46,8 @@
46
46
  "react-stately": "^3.31.1",
47
47
  "react-swipeable": "^7.0.1",
48
48
  "usehooks-ts": "^3.1.0",
49
- "@vygruppen/spor-design-tokens": "4.3.3",
50
49
  "@vygruppen/spor-icon-react": "4.5.3",
50
+ "@vygruppen/spor-design-tokens": "4.3.3",
51
51
  "@vygruppen/spor-loader": "0.7.0"
52
52
  },
53
53
  "devDependencies": {
@@ -0,0 +1,234 @@
1
+ "use client";
2
+ import {
3
+ Flex,
4
+ Menu as ChakraMenu,
5
+ MenuCheckboxItemProps,
6
+ MenuContentProps,
7
+ MenuItemGroupProps as ChakraMenuItemGroupProps,
8
+ MenuItemProps as ChakraMenuItemProps,
9
+ MenuRadioItemGroupProps,
10
+ MenuRadioItemProps,
11
+ MenuRootProps,
12
+ MenuSeparatorProps,
13
+ MenuTriggerItemProps as ChakraMenuTriggerItemProps,
14
+ Portal,
15
+ useMenuContext as useChakraMenuContext,
16
+ } from "@chakra-ui/react";
17
+ import {
18
+ DropdownDownFill18Icon,
19
+ DropdownDownFill24Icon,
20
+ } from "@vygruppen/spor-icon-react";
21
+ import { forwardRef, ReactNode } from "react";
22
+ import { createContext, useContext } from "react";
23
+
24
+ import { Button, ButtonProps, Checkbox } from "..";
25
+
26
+ type Variant = Pick<MenuRootProps, "variant">;
27
+
28
+ const CustomMenuContext = createContext<Variant>({
29
+ variant: "core",
30
+ });
31
+ export const useMenuContext = () => useContext(CustomMenuContext);
32
+
33
+ /**
34
+ * Menu component.
35
+ *
36
+ * Used to create an accessible dropdown menu.
37
+ *
38
+ * @example
39
+ * ```tsx
40
+ <Menu>
41
+ <MenuTrigger> Menu </MenuTrigger>
42
+ <MenuContent>
43
+ <MenuItem value="1"> Item 1 </MenuItem>
44
+ <MenuItem value="2"> Item 2 </MenuItem>
45
+ <MenuItem value="3"> Item 3 </MenuItem>
46
+ </MenuContent>
47
+ </Menu>
48
+ * ```
49
+ *
50
+ */
51
+
52
+ export const Menu = ({ children, ...props }: MenuRootProps) => {
53
+ return (
54
+ <CustomMenuContext.Provider
55
+ value={{
56
+ variant: props.variant,
57
+ }}
58
+ >
59
+ <ChakraMenu.Root {...props}>{children}</ChakraMenu.Root>
60
+ </CustomMenuContext.Provider>
61
+ );
62
+ };
63
+
64
+ export const MenuContent = forwardRef<HTMLDivElement, MenuContentProps>(
65
+ ({ children, ...props }, ref) => {
66
+ return (
67
+ <Portal>
68
+ <ChakraMenu.Positioner>
69
+ <ChakraMenu.Content ref={ref} {...props}>
70
+ {children}
71
+ </ChakraMenu.Content>
72
+ </ChakraMenu.Positioner>
73
+ </Portal>
74
+ );
75
+ },
76
+ );
77
+ MenuContent.displayName = "MenuContent";
78
+
79
+ export type MenuTriggerProps = {
80
+ /** An optional trigger button icon, rendered to the left of the label */
81
+ icon?: ReactNode;
82
+ } & Omit<ButtonProps, "variant" | "rightIcon" | "leftIcon">;
83
+
84
+ export const MenuTrigger = forwardRef<HTMLButtonElement, MenuTriggerProps>(
85
+ ({ icon, size, children, ...props }, ref) => {
86
+ const { variant } = useMenuContext();
87
+ const { open } = useChakraMenuContext();
88
+ const ChevronIcon =
89
+ size === "sm" ? DropdownDownFill18Icon : DropdownDownFill24Icon;
90
+
91
+ const getButtonVariant = (): ButtonProps["variant"] => {
92
+ if (variant === "floating") return "floating";
93
+ if (variant === "accent") return "secondary";
94
+ return "tertiary";
95
+ };
96
+
97
+ return (
98
+ <ChakraMenu.Trigger asChild ref={ref}>
99
+ <Button
100
+ leftIcon={icon}
101
+ variant={getButtonVariant()}
102
+ size={size}
103
+ {...props}
104
+ rightIcon={
105
+ <ChevronIcon
106
+ transform={open ? "rotate(180deg)" : undefined}
107
+ transition="transform 0.3s"
108
+ />
109
+ }
110
+ >
111
+ {children}
112
+ </Button>
113
+ </ChakraMenu.Trigger>
114
+ );
115
+ },
116
+ );
117
+ MenuTrigger.displayName = "MenuTrigger";
118
+
119
+ export type MenuItemProps = {
120
+ /** Display a command in the menu */
121
+ itemCommand?: string;
122
+ /* Display icon to the left */
123
+ leftIcon?: React.ReactNode;
124
+ /* Display icon to the right */
125
+ rightIcon?: React.ReactNode;
126
+ } & ChakraMenuItemProps;
127
+
128
+ export const MenuItem = forwardRef<HTMLDivElement, MenuItemProps>(
129
+ ({ itemCommand, children, value, leftIcon, rightIcon, ...props }, ref) => {
130
+ return (
131
+ <ChakraMenu.Item value={value} {...props} ref={ref}>
132
+ {leftIcon}
133
+ {children}
134
+ {itemCommand && (
135
+ <ChakraMenu.ItemCommand>{itemCommand}</ChakraMenu.ItemCommand>
136
+ )}
137
+ {rightIcon}
138
+ </ChakraMenu.Item>
139
+ );
140
+ },
141
+ );
142
+ MenuItem.displayName = "MenuItem";
143
+
144
+ export type MenuTriggerItemProps = {
145
+ /* Display icon to the left */
146
+ leftIcon?: React.ReactNode;
147
+ /* Display icon to the right */
148
+ rightIcon?: React.ReactNode;
149
+ } & ChakraMenuTriggerItemProps;
150
+
151
+ export const MenuTriggerItem = forwardRef<HTMLDivElement, MenuTriggerItemProps>(
152
+ ({ children, leftIcon, rightIcon, ...props }, ref) => {
153
+ return (
154
+ <ChakraMenu.TriggerItem {...props} ref={ref}>
155
+ {leftIcon}
156
+ {children}
157
+ {rightIcon}
158
+ </ChakraMenu.TriggerItem>
159
+ );
160
+ },
161
+ );
162
+ MenuTriggerItem.displayName = "MenuTriggerItem";
163
+
164
+ export const MenuRadioItemGroup = forwardRef<
165
+ HTMLDivElement,
166
+ MenuRadioItemGroupProps
167
+ >(({ children, ...props }) => {
168
+ return (
169
+ <ChakraMenu.RadioItemGroup {...props}>{children}</ChakraMenu.RadioItemGroup>
170
+ );
171
+ });
172
+ MenuRadioItemGroup.displayName = "MenuRadioItemGroup";
173
+
174
+ export const MenuRadioItem = forwardRef<HTMLDivElement, MenuRadioItemProps>(
175
+ ({ children, ...props }, ref) => {
176
+ return (
177
+ <ChakraMenu.RadioItem {...props} ref={ref}>
178
+ {children}
179
+ <Flex w="1.25rem" justify="center" align="center">
180
+ <ChakraMenu.ItemIndicator />
181
+ </Flex>
182
+ </ChakraMenu.RadioItem>
183
+ );
184
+ },
185
+ );
186
+ MenuRadioItem.displayName = "MenuRadioItem";
187
+
188
+ export type MenuItemGroupProps = {
189
+ /** Display group label */
190
+ label?: string;
191
+ } & ChakraMenuItemGroupProps;
192
+
193
+ export const MenuItemGroup = forwardRef<HTMLDivElement, MenuItemGroupProps>(
194
+ ({ children, label, ...props }, ref) => {
195
+ return (
196
+ <ChakraMenu.ItemGroup {...props} ref={ref}>
197
+ {label && (
198
+ <ChakraMenu.ItemGroupLabel>{label}</ChakraMenu.ItemGroupLabel>
199
+ )}
200
+ {children}
201
+ </ChakraMenu.ItemGroup>
202
+ );
203
+ },
204
+ );
205
+ MenuItemGroup.displayName = "MenuItemGroup";
206
+
207
+ export const MenuCheckboxItem = forwardRef<
208
+ HTMLDivElement,
209
+ MenuCheckboxItemProps
210
+ >(({ children, closeOnSelect = false, ...props }, ref) => {
211
+ return (
212
+ <ChakraMenu.CheckboxItem
213
+ {...props}
214
+ ref={ref}
215
+ closeOnSelect={closeOnSelect}
216
+ checked={props.checked}
217
+ onCheckedChange={props.onCheckedChange}
218
+ >
219
+ <Checkbox
220
+ checked={props.checked}
221
+ onCheckedChange={() => props.onCheckedChange}
222
+ >
223
+ {children}
224
+ </Checkbox>
225
+ </ChakraMenu.CheckboxItem>
226
+ );
227
+ });
228
+
229
+ MenuCheckboxItem.displayName = "MenuCheckboxItem";
230
+
231
+ export const MenuSeparator = forwardRef<MenuSeparatorProps>(({ ...props }) => {
232
+ return <ChakraMenu.Separator {...props} />;
233
+ });
234
+ MenuSeparator.displayName = "MenuSeparator";
@@ -9,6 +9,7 @@ export * from "./Field";
9
9
  export * from "./Fieldset";
10
10
  export * from "./Input";
11
11
  export * from "./ListBox";
12
+ export * from "./Menu";
12
13
  export * from "./NativeSelect";
13
14
  export * from "./NumericStepper";
14
15
  export * from "./PasswordInput";
@@ -285,3 +285,17 @@ export const comboboxAnatomy = arkComboboxAnatomy.extendWith(
285
285
  "indicatorGroup",
286
286
  "empty",
287
287
  );
288
+
289
+ export const menuAnatomy = createAnatomy("menu").parts(
290
+ "trigger",
291
+ "content",
292
+ "item",
293
+ "itemGroup",
294
+ "triggerItem",
295
+ "itemCommand",
296
+ "itemGroupLabel",
297
+ "separator",
298
+ "radioItem",
299
+ "triggerItem",
300
+ "checkboxItem",
301
+ );
@@ -17,6 +17,7 @@ import { lineIconSlotRecipe } from "./line-icon";
17
17
  import { listSlotRecipe } from "./list";
18
18
  import { listBoxSlotRecipe } from "./listbox";
19
19
  import { mediaControllerSlotRecipe } from "./media-controller-button";
20
+ import { menuSlotRecipe } from "./menu";
20
21
  import { nativeSelectSlotRecipe } from "./native-select";
21
22
  import { numericStepperRecipe } from "./numeric-stepper";
22
23
  import { paginationSlotRecipe } from "./pagination";
@@ -69,4 +70,5 @@ export const slotRecipes = {
69
70
  checkboxCard: choiceChipSlotRecipe,
70
71
  collapsible: collapsibleSlotRecipe,
71
72
  tooltip: popoverSlotRecipe,
73
+ menu: menuSlotRecipe,
72
74
  };
@@ -0,0 +1,111 @@
1
+ import { defineSlotRecipe } from "@chakra-ui/react";
2
+
3
+ import { menuAnatomy } from "./anatomy";
4
+
5
+ export const menuSlotRecipe = defineSlotRecipe({
6
+ className: "menu",
7
+ slots: menuAnatomy.keys(),
8
+ base: {
9
+ content: {
10
+ bg: "bg",
11
+ borderRadius: "sm",
12
+ boxShadow: "sm",
13
+ width: "fit-content",
14
+ padding: "1",
15
+
16
+ display: "flex",
17
+ flexDirection: "column",
18
+ gap: "0.5",
19
+ zIndex: "dropdown",
20
+
21
+ _open: {
22
+ animationStyle: "slide-fade-in",
23
+ animationDuration: "fast",
24
+ zIndex: "popover",
25
+ outline: "none",
26
+ },
27
+ },
28
+ itemGroup: {
29
+ display: "flex",
30
+ flexDirection: "column",
31
+ gap: "0.5",
32
+ },
33
+ item: {
34
+ borderRadius: "9px",
35
+ padding: "2",
36
+ display: "flex",
37
+ justifyContent: "space-between",
38
+ gap: 1.5,
39
+
40
+ _hover: {
41
+ backgroundColor: "accent.surface.hover",
42
+ },
43
+
44
+ "&:active": {
45
+ backgroundColor: "accent.surface.active",
46
+ },
47
+
48
+ _checked: {
49
+ backgroundColor: "accent.surface",
50
+ },
51
+
52
+ _highlighted: {
53
+ backgroundColor: "ghost.surface.hover",
54
+ },
55
+ },
56
+ itemGroupLabel: {
57
+ paddingY: "1",
58
+ fontWeight: "bold",
59
+ fontSize: "xs",
60
+ },
61
+ itemCommand: {
62
+ fontSize: "2xs",
63
+ },
64
+ separator: {
65
+ color: "outline",
66
+ },
67
+ radioItem: {
68
+ display: "flex",
69
+ justifyContent: "space-between",
70
+ gap: 2,
71
+ },
72
+ triggerItem: {
73
+ display: "flex",
74
+ justifyContent: "space-between",
75
+ gap: 1.5,
76
+ alignItems: "center",
77
+ },
78
+ checkboxItem: {
79
+ display: "flex",
80
+ gap: 2,
81
+ alignItems: "center",
82
+ width: "full",
83
+ },
84
+ },
85
+ variants: {
86
+ variant: {
87
+ core: {
88
+ content: {
89
+ border: "1px solid",
90
+ borderColor: "core.outline",
91
+ },
92
+ },
93
+ accent: {
94
+ content: {
95
+ border: "1px solid",
96
+ borderColor: "core.outline",
97
+ },
98
+ },
99
+ floating: {
100
+ content: {
101
+ border: "sm",
102
+ borderColor: "floating.outline",
103
+ boxShadow: "lg",
104
+ },
105
+ },
106
+ },
107
+ },
108
+ defaultVariants: {
109
+ variant: "core",
110
+ },
111
+ });