uikit-react-public 0.50.12 → 0.50.13
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/components/MenuNew/Menu.stories.d.ts +6 -0
- package/dist/components/MenuNew/MenuAccordion/MenuAccordionItem.d.ts +1 -1
- package/dist/components/MenuNew/MenuBaseItem.d.ts +3 -1
- package/dist/components/MenuNew/MenuItemText.d.ts +2 -1
- package/dist/components/MenuNew/PrimaryMenuItem.d.ts +1 -1
- package/dist/components/MenuNew/SecondaryMenuItem.d.ts +1 -1
- package/dist/index.js +3122 -3059
- package/lib/components/MenuNew/Menu.mdx +56 -0
- package/lib/components/MenuNew/Menu.stories.tsx +69 -0
- package/lib/components/MenuNew/MenuAccordion/MenuAccordion.tsx +8 -5
- package/lib/components/MenuNew/MenuAccordion/MenuAccordionItem.tsx +8 -1
- package/lib/components/MenuNew/MenuBaseItem.tsx +65 -8
- package/lib/components/MenuNew/MenuItemText.tsx +22 -5
- package/lib/components/MenuNew/PrimaryMenuItem.tsx +6 -1
- package/lib/components/MenuNew/SecondaryMenuItem.tsx +4 -1
- package/lib/components/MenuNew/__tests__/Menu.test.tsx +391 -1
- package/package.json +2 -2
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import { Meta, Canvas } from '@storybook/addon-docs/blocks';
|
|
2
|
+
import * as MenuStories from './Menu.stories';
|
|
3
|
+
|
|
4
|
+
<Meta of={MenuStories} />
|
|
5
|
+
|
|
6
|
+
# MenuNew
|
|
7
|
+
|
|
8
|
+
Compose navigation using `MenuNew.PrimaryItem`, `MenuNew.SecondaryItem`, and
|
|
9
|
+
`MenuNew.Accordion.Item`. Use `asChild` to supply an anchor or a router link
|
|
10
|
+
that forwards standard DOM props.
|
|
11
|
+
|
|
12
|
+
## Disabled items and badges
|
|
13
|
+
|
|
14
|
+
Set `disabled` to make an item unavailable. Disabled items use the theme's
|
|
15
|
+
disabled text colour and have no hover or active styling. They remain in
|
|
16
|
+
the tab order with visible focus styling so keyboard users can discover
|
|
17
|
+
unavailable options. They expose `aria-disabled` and block mouse and
|
|
18
|
+
keyboard activation, including child link handlers and navigation.
|
|
19
|
+
|
|
20
|
+
Explicit `tabIndex` values are preserved. With `asChild`, the child link
|
|
21
|
+
retains its normal focus behaviour; the wrapper does not add a second tab
|
|
22
|
+
stop.
|
|
23
|
+
|
|
24
|
+
The optional `badge` prop is independent of `disabled`: disabling an item
|
|
25
|
+
does not add a badge, and adding a badge does not disable the item. Pass the
|
|
26
|
+
standard `Badge` component with the desired variant. For the Figma
|
|
27
|
+
unavailable state, use `variant='caution-subtle'` and `icon`.
|
|
28
|
+
|
|
29
|
+
```tsx
|
|
30
|
+
import { Badge, MenuNew } from '@dev-and-test/uikit-react';
|
|
31
|
+
|
|
32
|
+
<MenuNew.Accordion.Item
|
|
33
|
+
disabled
|
|
34
|
+
asChild
|
|
35
|
+
badge={
|
|
36
|
+
<Badge
|
|
37
|
+
variant='caution-subtle'
|
|
38
|
+
icon
|
|
39
|
+
>
|
|
40
|
+
Temporary unavailable
|
|
41
|
+
</Badge>
|
|
42
|
+
}
|
|
43
|
+
>
|
|
44
|
+
<a href='/service'>Menu subitem</a>
|
|
45
|
+
</MenuNew.Accordion.Item>;
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
`disabled` and `badge` are also available on `MenuNew.BaseItem` and
|
|
49
|
+
`MenuNew.Accordion` (its trigger). Disabling an accordion trigger prevents
|
|
50
|
+
toggling it; it does not disable its children. `MenuNew.ActionButton`
|
|
51
|
+
retains the standard `Button` disabled behaviour.
|
|
52
|
+
|
|
53
|
+
Badges retain their own colours on disabled items. Collapsed primary items
|
|
54
|
+
hide badges along with other trailing content.
|
|
55
|
+
|
|
56
|
+
<Canvas of={MenuStories.DisabledItems} />
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import type { Meta, StoryObj } from '@storybook/react-vite';
|
|
2
|
+
import Menu from './Menu';
|
|
3
|
+
import Badge from '../Badge';
|
|
4
|
+
import Icon from '../Icon';
|
|
5
|
+
|
|
6
|
+
const meta: Meta<typeof Menu> = {
|
|
7
|
+
title: 'Components/MenuNew',
|
|
8
|
+
component: Menu,
|
|
9
|
+
parameters: { layout: 'padded' },
|
|
10
|
+
tags: ['!autodocs'],
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
export default meta;
|
|
14
|
+
type Story = StoryObj<typeof meta>;
|
|
15
|
+
|
|
16
|
+
export const DisabledItems: Story = {
|
|
17
|
+
render: () => (
|
|
18
|
+
<Menu>
|
|
19
|
+
<Menu.PrimaryItem
|
|
20
|
+
icon={<Icon.Home />}
|
|
21
|
+
asChild
|
|
22
|
+
>
|
|
23
|
+
<a href='#home'>Home</a>
|
|
24
|
+
</Menu.PrimaryItem>
|
|
25
|
+
<Menu.PrimaryItem
|
|
26
|
+
icon={<Icon.Calendar />}
|
|
27
|
+
disabled
|
|
28
|
+
>
|
|
29
|
+
Appointments
|
|
30
|
+
</Menu.PrimaryItem>
|
|
31
|
+
<Menu.Accordion
|
|
32
|
+
label='Services'
|
|
33
|
+
icon={<Icon.BookOpen />}
|
|
34
|
+
defaultExpanded
|
|
35
|
+
>
|
|
36
|
+
<Menu.Accordion.Item
|
|
37
|
+
disabled
|
|
38
|
+
asChild
|
|
39
|
+
badge={
|
|
40
|
+
<Badge
|
|
41
|
+
variant='caution-subtle'
|
|
42
|
+
icon
|
|
43
|
+
>
|
|
44
|
+
Temporary unavailable
|
|
45
|
+
</Badge>
|
|
46
|
+
}
|
|
47
|
+
>
|
|
48
|
+
<a href='#service'>Menu subitem</a>
|
|
49
|
+
</Menu.Accordion.Item>
|
|
50
|
+
<Menu.Accordion.Item disabled>
|
|
51
|
+
Disabled without a badge
|
|
52
|
+
</Menu.Accordion.Item>
|
|
53
|
+
<Menu.Accordion.Item
|
|
54
|
+
badge={<Badge variant='success'>New</Badge>}
|
|
55
|
+
asChild
|
|
56
|
+
>
|
|
57
|
+
<a href='#library'>Library</a>
|
|
58
|
+
</Menu.Accordion.Item>
|
|
59
|
+
</Menu.Accordion>
|
|
60
|
+
<Menu.SecondaryItem
|
|
61
|
+
disabled
|
|
62
|
+
externalLink
|
|
63
|
+
asChild
|
|
64
|
+
>
|
|
65
|
+
<a href='https://www.ucl.ac.uk/'>Support</a>
|
|
66
|
+
</Menu.SecondaryItem>
|
|
67
|
+
</Menu>
|
|
68
|
+
),
|
|
69
|
+
};
|
|
@@ -30,6 +30,7 @@ const MenuAccordion = ({
|
|
|
30
30
|
label,
|
|
31
31
|
children,
|
|
32
32
|
defaultExpanded = false,
|
|
33
|
+
disabled = false,
|
|
33
34
|
onClick,
|
|
34
35
|
...props
|
|
35
36
|
}: MenuAccordionProps) => {
|
|
@@ -44,10 +45,6 @@ const MenuAccordion = ({
|
|
|
44
45
|
@media screen and (min-width: ${theme.breakpoints.tablet}px) {
|
|
45
46
|
padding: 0 ${theme.padding.p16};
|
|
46
47
|
}
|
|
47
|
-
|
|
48
|
-
&:hover {
|
|
49
|
-
background-color: ${theme.colour.fill.brandSubtleHover};
|
|
50
|
-
}
|
|
51
48
|
`;
|
|
52
49
|
|
|
53
50
|
const style = cx(NAME, baseStyle, className);
|
|
@@ -105,12 +102,18 @@ const MenuAccordion = ({
|
|
|
105
102
|
iconPosition='left'
|
|
106
103
|
hoverMode='background'
|
|
107
104
|
onClick={handleClick}
|
|
105
|
+
disabled={disabled}
|
|
108
106
|
endAdornment={<MenuAccordionChevron expanded={expanded} />}
|
|
109
107
|
aria-expanded={expanded}
|
|
110
108
|
{...props}
|
|
111
109
|
>
|
|
112
110
|
<span className={contentStyle}>
|
|
113
|
-
<MenuItemText
|
|
111
|
+
<MenuItemText
|
|
112
|
+
className={textStyle}
|
|
113
|
+
disabled={disabled}
|
|
114
|
+
>
|
|
115
|
+
{label}
|
|
116
|
+
</MenuItemText>
|
|
114
117
|
</span>
|
|
115
118
|
</MenuBaseItem>
|
|
116
119
|
<div
|
|
@@ -18,6 +18,7 @@ const MenuAccordionItem = ({
|
|
|
18
18
|
testId = NAME,
|
|
19
19
|
className,
|
|
20
20
|
children,
|
|
21
|
+
disabled = false,
|
|
21
22
|
...props
|
|
22
23
|
}: MenuAccordionItemProps) => {
|
|
23
24
|
const [theme] = useTheme();
|
|
@@ -39,10 +40,16 @@ const MenuAccordionItem = ({
|
|
|
39
40
|
className={style}
|
|
40
41
|
iconPosition='left'
|
|
41
42
|
asChild={asChild}
|
|
43
|
+
disabled={disabled}
|
|
42
44
|
data-testid={testId}
|
|
43
45
|
{...props}
|
|
44
46
|
>
|
|
45
|
-
<MenuItemText
|
|
47
|
+
<MenuItemText
|
|
48
|
+
asChild={asChild}
|
|
49
|
+
disabled={disabled}
|
|
50
|
+
>
|
|
51
|
+
{children}
|
|
52
|
+
</MenuItemText>
|
|
46
53
|
</MenuBaseItem>
|
|
47
54
|
);
|
|
48
55
|
};
|
|
@@ -1,4 +1,9 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import {
|
|
2
|
+
HTMLAttributes,
|
|
3
|
+
KeyboardEvent,
|
|
4
|
+
ReactNode,
|
|
5
|
+
SyntheticEvent,
|
|
6
|
+
} from 'react';
|
|
2
7
|
import { css, cx } from '@emotion/css';
|
|
3
8
|
import { useTheme } from '../../theme';
|
|
4
9
|
|
|
@@ -9,6 +14,8 @@ export interface MenuBaseItemProps extends HTMLAttributes<HTMLDivElement> {
|
|
|
9
14
|
hoverMode?: 'background' | 'underline';
|
|
10
15
|
asChild?: boolean;
|
|
11
16
|
active?: boolean;
|
|
17
|
+
disabled?: boolean;
|
|
18
|
+
badge?: ReactNode;
|
|
12
19
|
}
|
|
13
20
|
|
|
14
21
|
const MenuBaseItem = ({
|
|
@@ -19,17 +26,39 @@ const MenuBaseItem = ({
|
|
|
19
26
|
children,
|
|
20
27
|
onClick,
|
|
21
28
|
onKeyDown,
|
|
29
|
+
onClickCapture,
|
|
30
|
+
onAuxClickCapture,
|
|
31
|
+
onKeyDownCapture,
|
|
32
|
+
onContextMenuCapture,
|
|
22
33
|
role,
|
|
23
34
|
tabIndex,
|
|
24
35
|
hoverMode = 'background',
|
|
25
36
|
asChild = false,
|
|
26
37
|
active = false,
|
|
38
|
+
disabled = false,
|
|
39
|
+
badge,
|
|
27
40
|
...props
|
|
28
41
|
}: MenuBaseItemProps) => {
|
|
29
42
|
const [theme] = useTheme();
|
|
30
43
|
const isInteractive = typeof onClick === 'function';
|
|
31
44
|
const shouldUseButtonSemantics = !asChild;
|
|
32
45
|
|
|
46
|
+
const preventInteraction = (event: SyntheticEvent<HTMLDivElement>) => {
|
|
47
|
+
event.preventDefault();
|
|
48
|
+
event.stopPropagation();
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
const handleKeyDownCapture = (event: KeyboardEvent<HTMLDivElement>) => {
|
|
52
|
+
if (disabled) {
|
|
53
|
+
if (event.key === 'Enter' || event.key === ' ') {
|
|
54
|
+
preventInteraction(event);
|
|
55
|
+
}
|
|
56
|
+
return;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
onKeyDownCapture?.(event);
|
|
60
|
+
};
|
|
61
|
+
|
|
33
62
|
const handleKeyDown = (event: KeyboardEvent<HTMLDivElement>) => {
|
|
34
63
|
onKeyDown?.(event);
|
|
35
64
|
|
|
@@ -93,14 +122,28 @@ const MenuBaseItem = ({
|
|
|
93
122
|
}
|
|
94
123
|
`;
|
|
95
124
|
|
|
125
|
+
const disabledStyle = css`
|
|
126
|
+
&[aria-disabled='true'] {
|
|
127
|
+
cursor: not-allowed;
|
|
128
|
+
color: ${theme.colour.text.disabled};
|
|
129
|
+
}
|
|
130
|
+
`;
|
|
131
|
+
|
|
96
132
|
const style = cx(
|
|
97
133
|
baseStyle,
|
|
98
|
-
active && activeStyle,
|
|
99
|
-
hoverMode === 'underline' && underlineStyleOnHoverStyle,
|
|
100
|
-
hoverMode === 'background' && backgroundOnHoverStyle,
|
|
134
|
+
!disabled && active && activeStyle,
|
|
135
|
+
!disabled && hoverMode === 'underline' && underlineStyleOnHoverStyle,
|
|
136
|
+
!disabled && hoverMode === 'background' && backgroundOnHoverStyle,
|
|
137
|
+
disabled && disabledStyle,
|
|
101
138
|
className
|
|
102
139
|
);
|
|
103
140
|
|
|
141
|
+
const contentStyle = css`
|
|
142
|
+
display: inline-flex;
|
|
143
|
+
align-items: center;
|
|
144
|
+
gap: ${theme.margin.m8};
|
|
145
|
+
`;
|
|
146
|
+
|
|
104
147
|
const endAdornmentStyle = css`
|
|
105
148
|
margin-left: auto;
|
|
106
149
|
display: inline-flex;
|
|
@@ -110,15 +153,29 @@ const MenuBaseItem = ({
|
|
|
110
153
|
|
|
111
154
|
return (
|
|
112
155
|
<div
|
|
156
|
+
{...props}
|
|
113
157
|
className={style}
|
|
114
|
-
onClick={onClick}
|
|
115
|
-
onKeyDown={handleKeyDown}
|
|
158
|
+
onClick={disabled ? undefined : onClick}
|
|
159
|
+
onKeyDown={disabled ? undefined : handleKeyDown}
|
|
160
|
+
onClickCapture={disabled ? preventInteraction : onClickCapture}
|
|
161
|
+
onAuxClickCapture={disabled ? preventInteraction : onAuxClickCapture}
|
|
162
|
+
onKeyDownCapture={handleKeyDownCapture}
|
|
163
|
+
onContextMenuCapture={
|
|
164
|
+
disabled ? preventInteraction : onContextMenuCapture
|
|
165
|
+
}
|
|
116
166
|
role={role ?? (shouldUseButtonSemantics ? 'button' : undefined)}
|
|
117
167
|
tabIndex={tabIndex ?? (shouldUseButtonSemantics ? 0 : undefined)}
|
|
118
|
-
{
|
|
168
|
+
aria-disabled={disabled || props['aria-disabled']}
|
|
119
169
|
>
|
|
120
170
|
{iconPosition === 'left' && icon}
|
|
121
|
-
{
|
|
171
|
+
{badge != null ? (
|
|
172
|
+
<span className={contentStyle}>
|
|
173
|
+
{children}
|
|
174
|
+
{badge}
|
|
175
|
+
</span>
|
|
176
|
+
) : (
|
|
177
|
+
children
|
|
178
|
+
)}
|
|
122
179
|
{iconPosition === 'right' && icon}
|
|
123
180
|
{endAdornment && (
|
|
124
181
|
<span className={endAdornmentStyle}>{endAdornment}</span>
|
|
@@ -1,12 +1,14 @@
|
|
|
1
1
|
import { css, cx } from '@emotion/css';
|
|
2
2
|
import {
|
|
3
3
|
Children,
|
|
4
|
+
HTMLAttributes,
|
|
4
5
|
ReactElement,
|
|
5
6
|
ReactNode,
|
|
6
7
|
cloneElement,
|
|
7
8
|
isValidElement,
|
|
8
9
|
} from 'react';
|
|
9
10
|
import Text from '../Text';
|
|
11
|
+
import { useTheme } from '../../theme';
|
|
10
12
|
|
|
11
13
|
export const NAME = 'ucl-uikit-menu__item-text';
|
|
12
14
|
|
|
@@ -14,6 +16,7 @@ export interface MenuItemTextProps {
|
|
|
14
16
|
asChild?: boolean;
|
|
15
17
|
className?: string;
|
|
16
18
|
visuallyHidden?: boolean;
|
|
19
|
+
disabled?: boolean;
|
|
17
20
|
children?: ReactNode;
|
|
18
21
|
}
|
|
19
22
|
|
|
@@ -21,8 +24,11 @@ const MenuItemText = ({
|
|
|
21
24
|
asChild = false,
|
|
22
25
|
className,
|
|
23
26
|
visuallyHidden = false,
|
|
27
|
+
disabled = false,
|
|
24
28
|
children,
|
|
25
29
|
}: MenuItemTextProps) => {
|
|
30
|
+
const [theme] = useTheme();
|
|
31
|
+
|
|
26
32
|
const asChildResetStyle = css`
|
|
27
33
|
font: inherit;
|
|
28
34
|
color: inherit;
|
|
@@ -60,7 +66,17 @@ const MenuItemText = ({
|
|
|
60
66
|
border: 0;
|
|
61
67
|
`;
|
|
62
68
|
|
|
63
|
-
const
|
|
69
|
+
const disabledStyle = css`
|
|
70
|
+
color: ${theme.colour.text.disabled};
|
|
71
|
+
`;
|
|
72
|
+
|
|
73
|
+
const textStyle = cx(NAME, disabled && disabledStyle, className);
|
|
74
|
+
const style = cx(
|
|
75
|
+
NAME,
|
|
76
|
+
visuallyHidden && visuallyHiddenStyle,
|
|
77
|
+
disabled && disabledStyle,
|
|
78
|
+
className
|
|
79
|
+
);
|
|
64
80
|
|
|
65
81
|
if (asChild) {
|
|
66
82
|
const visuallyHiddenAsChildStyle = css`
|
|
@@ -69,14 +85,15 @@ const MenuItemText = ({
|
|
|
69
85
|
line-height: 0;
|
|
70
86
|
`;
|
|
71
87
|
|
|
72
|
-
const child = Children.only(children) as ReactElement<
|
|
73
|
-
|
|
74
|
-
|
|
88
|
+
const child = Children.only(children) as ReactElement<
|
|
89
|
+
HTMLAttributes<HTMLElement>
|
|
90
|
+
>;
|
|
75
91
|
|
|
76
92
|
if (isValidElement(child)) {
|
|
77
93
|
return (
|
|
78
|
-
<Text className={
|
|
94
|
+
<Text className={textStyle}>
|
|
79
95
|
{cloneElement(child, {
|
|
96
|
+
...(disabled ? { 'aria-disabled': true } : {}),
|
|
80
97
|
className: cx(
|
|
81
98
|
asChildResetStyle,
|
|
82
99
|
visuallyHidden && visuallyHiddenAsChildStyle,
|
|
@@ -23,6 +23,8 @@ const PrimaryMenuItem = ({
|
|
|
23
23
|
trailingContent,
|
|
24
24
|
icon: iconProp,
|
|
25
25
|
active = false,
|
|
26
|
+
disabled = false,
|
|
27
|
+
badge,
|
|
26
28
|
...props
|
|
27
29
|
}: PrimaryMenuItemProps) => {
|
|
28
30
|
const [theme] = useTheme();
|
|
@@ -96,7 +98,7 @@ const PrimaryMenuItem = ({
|
|
|
96
98
|
NAME,
|
|
97
99
|
baseStyle,
|
|
98
100
|
collapsed && collapsedStyle,
|
|
99
|
-
collapsed && active && collapsedActiveStyle,
|
|
101
|
+
collapsed && active && !disabled && collapsedActiveStyle,
|
|
100
102
|
className
|
|
101
103
|
);
|
|
102
104
|
|
|
@@ -115,6 +117,8 @@ const PrimaryMenuItem = ({
|
|
|
115
117
|
iconPosition='left'
|
|
116
118
|
icon={iconProp}
|
|
117
119
|
active={!collapsed && active}
|
|
120
|
+
disabled={disabled}
|
|
121
|
+
badge={collapsed ? undefined : badge}
|
|
118
122
|
aria-current={active ? 'page' : props['aria-current']}
|
|
119
123
|
{...props}
|
|
120
124
|
>
|
|
@@ -123,6 +127,7 @@ const PrimaryMenuItem = ({
|
|
|
123
127
|
asChild={asChild}
|
|
124
128
|
className={textStyle}
|
|
125
129
|
visuallyHidden={collapsed}
|
|
130
|
+
disabled={disabled}
|
|
126
131
|
>
|
|
127
132
|
{children}
|
|
128
133
|
</MenuItemText>
|
|
@@ -20,6 +20,7 @@ const SecondaryMenuItem = ({
|
|
|
20
20
|
children,
|
|
21
21
|
asChild,
|
|
22
22
|
externalLink,
|
|
23
|
+
disabled = false,
|
|
23
24
|
...props
|
|
24
25
|
}: SecondaryMenuItemProps) => {
|
|
25
26
|
const [theme] = useTheme();
|
|
@@ -50,12 +51,14 @@ const SecondaryMenuItem = ({
|
|
|
50
51
|
className={style}
|
|
51
52
|
asChild={asChild}
|
|
52
53
|
hoverMode='underline'
|
|
54
|
+
disabled={disabled}
|
|
53
55
|
{...props}
|
|
54
56
|
>
|
|
55
57
|
<span className={contentStyle}>
|
|
56
58
|
<MenuItemText
|
|
57
59
|
asChild={asChild}
|
|
58
|
-
className={textStyle}
|
|
60
|
+
className={disabled ? undefined : textStyle}
|
|
61
|
+
disabled={disabled}
|
|
59
62
|
>
|
|
60
63
|
{children}
|
|
61
64
|
</MenuItemText>
|