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
|
@@ -1,7 +1,13 @@
|
|
|
1
1
|
import { describe, expect, test, vi } from 'vitest';
|
|
2
2
|
import { fireEvent, render, screen } from '@testing-library/react';
|
|
3
|
+
import userEvent from '@testing-library/user-event';
|
|
4
|
+
import { Link as WouterLink } from 'wouter';
|
|
5
|
+
import { css } from '@emotion/css';
|
|
3
6
|
import Menu from '../Menu';
|
|
4
|
-
import
|
|
7
|
+
import MenuItemText from '../MenuItemText';
|
|
8
|
+
import Badge from '../../Badge';
|
|
9
|
+
import Icon from '../../Icon';
|
|
10
|
+
import { ThemeContextProvider, themes } from '../../../theme/useTheme';
|
|
5
11
|
|
|
6
12
|
const wrap = (ui: React.ReactElement) =>
|
|
7
13
|
render(<ThemeContextProvider>{ui}</ThemeContextProvider>);
|
|
@@ -13,6 +19,29 @@ describe('Menu', () => {
|
|
|
13
19
|
expect(screen.getByTestId('ucl-uikit-menu')).toBeInTheDocument();
|
|
14
20
|
});
|
|
15
21
|
|
|
22
|
+
test.each([false, true])(
|
|
23
|
+
'custom text styles override disabled styles with asChild=%s',
|
|
24
|
+
(asChild) => {
|
|
25
|
+
const className = css`
|
|
26
|
+
color: rgb(12, 34, 56);
|
|
27
|
+
`;
|
|
28
|
+
|
|
29
|
+
wrap(
|
|
30
|
+
<MenuItemText
|
|
31
|
+
disabled
|
|
32
|
+
asChild={asChild}
|
|
33
|
+
className={className}
|
|
34
|
+
>
|
|
35
|
+
{asChild ? <a href='/service'>Service</a> : 'Service'}
|
|
36
|
+
</MenuItemText>
|
|
37
|
+
);
|
|
38
|
+
|
|
39
|
+
expect(
|
|
40
|
+
screen.getByText('Service').closest('.ucl-uikit-menu__item-text')
|
|
41
|
+
).toHaveStyle({ color: 'rgb(12, 34, 56)' });
|
|
42
|
+
}
|
|
43
|
+
);
|
|
44
|
+
|
|
16
45
|
test('renders heading item', () => {
|
|
17
46
|
wrap(
|
|
18
47
|
<Menu>
|
|
@@ -420,4 +449,365 @@ describe('Menu', () => {
|
|
|
420
449
|
expect(icon).toHaveAttribute('aria-hidden', 'true');
|
|
421
450
|
expect(icon).toHaveAttribute('focusable', 'false');
|
|
422
451
|
});
|
|
452
|
+
|
|
453
|
+
describe.each([
|
|
454
|
+
{ name: 'base item', Item: Menu.BaseItem },
|
|
455
|
+
{ name: 'primary item', Item: Menu.PrimaryItem },
|
|
456
|
+
{ name: 'secondary item', Item: Menu.SecondaryItem },
|
|
457
|
+
{ name: 'accordion item', Item: Menu.Accordion.Item },
|
|
458
|
+
])('$name disabled state and badge', ({ Item }) => {
|
|
459
|
+
test('remains tabbable while blocking click and keyboard handlers', async () => {
|
|
460
|
+
const user = userEvent.setup();
|
|
461
|
+
const onClick = vi.fn();
|
|
462
|
+
const onKeyDown = vi.fn();
|
|
463
|
+
const onClickCapture = vi.fn();
|
|
464
|
+
const onKeyDownCapture = vi.fn();
|
|
465
|
+
|
|
466
|
+
wrap(
|
|
467
|
+
<Menu>
|
|
468
|
+
<Item
|
|
469
|
+
disabled
|
|
470
|
+
active
|
|
471
|
+
onClick={onClick}
|
|
472
|
+
onKeyDown={onKeyDown}
|
|
473
|
+
onClickCapture={onClickCapture}
|
|
474
|
+
onKeyDownCapture={onKeyDownCapture}
|
|
475
|
+
>
|
|
476
|
+
Unavailable
|
|
477
|
+
</Item>
|
|
478
|
+
<Menu.PrimaryItem>Available</Menu.PrimaryItem>
|
|
479
|
+
</Menu>
|
|
480
|
+
);
|
|
481
|
+
|
|
482
|
+
const item = screen.getByRole('button', { name: 'Unavailable' });
|
|
483
|
+
expect(item).toHaveAttribute('aria-disabled', 'true');
|
|
484
|
+
expect(item).toHaveAttribute('tabindex', '0');
|
|
485
|
+
expect(item).not.toHaveAttribute('disabled');
|
|
486
|
+
expect(item).toHaveStyle({ cursor: 'not-allowed' });
|
|
487
|
+
expect(screen.queryByTestId('ucl-uikit-badge')).not.toBeInTheDocument();
|
|
488
|
+
const generatedClass = Array.from(item.classList).find((className) =>
|
|
489
|
+
className.startsWith('css-')
|
|
490
|
+
);
|
|
491
|
+
const cssRules = Array.from(document.styleSheets)
|
|
492
|
+
.flatMap((styleSheet) => Array.from(styleSheet.cssRules))
|
|
493
|
+
.map((rule) => rule.cssText)
|
|
494
|
+
.join(' ');
|
|
495
|
+
expect(cssRules).not.toContain(`.${generatedClass}:hover`);
|
|
496
|
+
expect(cssRules).not.toContain(`.${generatedClass}::before`);
|
|
497
|
+
|
|
498
|
+
await user.tab();
|
|
499
|
+
expect(item).toHaveFocus();
|
|
500
|
+
await user.click(item);
|
|
501
|
+
await user.keyboard('{Enter} ');
|
|
502
|
+
expect(onClick).not.toHaveBeenCalled();
|
|
503
|
+
expect(onKeyDown).not.toHaveBeenCalled();
|
|
504
|
+
expect(onClickCapture).not.toHaveBeenCalled();
|
|
505
|
+
expect(onKeyDownCapture).not.toHaveBeenCalled();
|
|
506
|
+
|
|
507
|
+
await user.tab();
|
|
508
|
+
expect(screen.getByRole('button', { name: 'Available' })).toHaveFocus();
|
|
509
|
+
await user.tab({ shift: true });
|
|
510
|
+
expect(item).toHaveFocus();
|
|
511
|
+
});
|
|
512
|
+
|
|
513
|
+
test.each([false, true])(
|
|
514
|
+
'renders a badge independently of disabled=%s',
|
|
515
|
+
(disabled) => {
|
|
516
|
+
const onClick = vi.fn();
|
|
517
|
+
wrap(
|
|
518
|
+
<Menu>
|
|
519
|
+
<Item
|
|
520
|
+
disabled={disabled}
|
|
521
|
+
onClick={onClick}
|
|
522
|
+
badge={
|
|
523
|
+
<Badge
|
|
524
|
+
variant='caution-subtle'
|
|
525
|
+
icon
|
|
526
|
+
>
|
|
527
|
+
Temporary unavailable
|
|
528
|
+
</Badge>
|
|
529
|
+
}
|
|
530
|
+
>
|
|
531
|
+
Service
|
|
532
|
+
</Item>
|
|
533
|
+
</Menu>
|
|
534
|
+
);
|
|
535
|
+
|
|
536
|
+
const item = screen.getByRole('button', {
|
|
537
|
+
name: 'Service Temporary unavailable',
|
|
538
|
+
});
|
|
539
|
+
const badge = screen.getByTestId('ucl-uikit-badge');
|
|
540
|
+
expect(badge).toHaveTextContent('Temporary unavailable');
|
|
541
|
+
expect(badge.parentElement).toHaveStyle({ gap: '8px' });
|
|
542
|
+
expect(item).not.toHaveAttribute('badge');
|
|
543
|
+
|
|
544
|
+
fireEvent.click(badge);
|
|
545
|
+
expect(onClick).toHaveBeenCalledTimes(disabled ? 0 : 1);
|
|
546
|
+
}
|
|
547
|
+
);
|
|
548
|
+
|
|
549
|
+
test('restores interaction when enabled again', () => {
|
|
550
|
+
const onClick = vi.fn();
|
|
551
|
+
const onClickCapture = vi.fn();
|
|
552
|
+
const renderItem = (disabled: boolean) => (
|
|
553
|
+
<ThemeContextProvider>
|
|
554
|
+
<Menu>
|
|
555
|
+
<Item
|
|
556
|
+
disabled={disabled}
|
|
557
|
+
onClick={onClick}
|
|
558
|
+
onClickCapture={onClickCapture}
|
|
559
|
+
>
|
|
560
|
+
Service
|
|
561
|
+
</Item>
|
|
562
|
+
</Menu>
|
|
563
|
+
</ThemeContextProvider>
|
|
564
|
+
);
|
|
565
|
+
const { rerender } = render(renderItem(true));
|
|
566
|
+
rerender(renderItem(false));
|
|
567
|
+
|
|
568
|
+
const item = screen.getByRole('button', { name: 'Service' });
|
|
569
|
+
expect(item).not.toHaveAttribute('aria-disabled');
|
|
570
|
+
expect(item).toHaveAttribute('tabindex', '0');
|
|
571
|
+
fireEvent.click(item);
|
|
572
|
+
fireEvent.keyDown(item, { key: 'Enter' });
|
|
573
|
+
fireEvent.keyDown(item, { key: ' ' });
|
|
574
|
+
expect(onClick).toHaveBeenCalledTimes(3);
|
|
575
|
+
expect(onClickCapture).toHaveBeenCalledTimes(3);
|
|
576
|
+
});
|
|
577
|
+
});
|
|
578
|
+
|
|
579
|
+
describe.each([
|
|
580
|
+
{ name: 'primary item', Item: Menu.PrimaryItem },
|
|
581
|
+
{ name: 'secondary item', Item: Menu.SecondaryItem },
|
|
582
|
+
{ name: 'accordion item', Item: Menu.Accordion.Item },
|
|
583
|
+
])('disabled $name asChild', ({ Item }) => {
|
|
584
|
+
test.each(['native', 'router'])(
|
|
585
|
+
'keeps a single tab stop while blocking %s link handlers and navigation',
|
|
586
|
+
async (linkType) => {
|
|
587
|
+
const user = userEvent.setup();
|
|
588
|
+
const onClick = vi.fn();
|
|
589
|
+
const onKeyDown = vi.fn();
|
|
590
|
+
const onClickCapture = vi.fn();
|
|
591
|
+
const linkProps = {
|
|
592
|
+
onClick,
|
|
593
|
+
onKeyDown,
|
|
594
|
+
onClickCapture,
|
|
595
|
+
children: 'Unavailable',
|
|
596
|
+
};
|
|
597
|
+
|
|
598
|
+
wrap(
|
|
599
|
+
<Menu>
|
|
600
|
+
<Item
|
|
601
|
+
disabled
|
|
602
|
+
asChild
|
|
603
|
+
badge={<Badge variant='caution-subtle'>Unavailable now</Badge>}
|
|
604
|
+
>
|
|
605
|
+
{linkType === 'native' ? (
|
|
606
|
+
<a
|
|
607
|
+
href='/unavailable'
|
|
608
|
+
{...linkProps}
|
|
609
|
+
/>
|
|
610
|
+
) : (
|
|
611
|
+
<WouterLink
|
|
612
|
+
to='/unavailable'
|
|
613
|
+
{...linkProps}
|
|
614
|
+
/>
|
|
615
|
+
)}
|
|
616
|
+
</Item>
|
|
617
|
+
<Menu.PrimaryItem>Available</Menu.PrimaryItem>
|
|
618
|
+
</Menu>
|
|
619
|
+
);
|
|
620
|
+
|
|
621
|
+
const link = screen.getByRole('link', { name: 'Unavailable' });
|
|
622
|
+
expect(link).toHaveAttribute('aria-disabled', 'true');
|
|
623
|
+
expect(link.tabIndex).toBe(0);
|
|
624
|
+
await user.tab();
|
|
625
|
+
expect(link).toHaveFocus();
|
|
626
|
+
expect(fireEvent.click(link)).toBe(false);
|
|
627
|
+
expect(fireEvent.click(link, { ctrlKey: true })).toBe(false);
|
|
628
|
+
expect(
|
|
629
|
+
fireEvent(
|
|
630
|
+
link,
|
|
631
|
+
new MouseEvent('auxclick', {
|
|
632
|
+
bubbles: true,
|
|
633
|
+
cancelable: true,
|
|
634
|
+
button: 1,
|
|
635
|
+
})
|
|
636
|
+
)
|
|
637
|
+
).toBe(false);
|
|
638
|
+
expect(fireEvent.contextMenu(link)).toBe(false);
|
|
639
|
+
expect(fireEvent.keyDown(link, { key: 'Enter' })).toBe(false);
|
|
640
|
+
expect(fireEvent.keyDown(link, { key: ' ' })).toBe(false);
|
|
641
|
+
await user.keyboard('{Enter} ');
|
|
642
|
+
expect(onClick).not.toHaveBeenCalled();
|
|
643
|
+
expect(onKeyDown).not.toHaveBeenCalled();
|
|
644
|
+
expect(onClickCapture).not.toHaveBeenCalled();
|
|
645
|
+
|
|
646
|
+
await user.tab();
|
|
647
|
+
expect(screen.getByRole('button', { name: 'Available' })).toHaveFocus();
|
|
648
|
+
await user.tab({ shift: true });
|
|
649
|
+
expect(link).toHaveFocus();
|
|
650
|
+
}
|
|
651
|
+
);
|
|
652
|
+
});
|
|
653
|
+
|
|
654
|
+
test('restores asChild link props when enabled again', () => {
|
|
655
|
+
const onClick = vi.fn((event) => event.preventDefault());
|
|
656
|
+
const renderItem = (disabled: boolean) => (
|
|
657
|
+
<ThemeContextProvider>
|
|
658
|
+
<Menu.PrimaryItem
|
|
659
|
+
disabled={disabled}
|
|
660
|
+
asChild
|
|
661
|
+
>
|
|
662
|
+
<a
|
|
663
|
+
href='/service'
|
|
664
|
+
tabIndex={0}
|
|
665
|
+
onClick={onClick}
|
|
666
|
+
>
|
|
667
|
+
Service
|
|
668
|
+
</a>
|
|
669
|
+
</Menu.PrimaryItem>
|
|
670
|
+
</ThemeContextProvider>
|
|
671
|
+
);
|
|
672
|
+
const { rerender } = render(renderItem(true));
|
|
673
|
+
rerender(renderItem(false));
|
|
674
|
+
const link = screen.getByRole('link', { name: 'Service' });
|
|
675
|
+
|
|
676
|
+
expect(link).toHaveAttribute('tabindex', '0');
|
|
677
|
+
expect(link).not.toHaveAttribute('aria-disabled');
|
|
678
|
+
fireEvent.click(link);
|
|
679
|
+
expect(onClick).toHaveBeenCalledOnce();
|
|
680
|
+
});
|
|
681
|
+
|
|
682
|
+
test('disabled items preserve explicit tabIndex overrides', async () => {
|
|
683
|
+
const user = userEvent.setup();
|
|
684
|
+
wrap(
|
|
685
|
+
<Menu>
|
|
686
|
+
<Menu.PrimaryItem
|
|
687
|
+
disabled
|
|
688
|
+
tabIndex={-1}
|
|
689
|
+
>
|
|
690
|
+
Unavailable action
|
|
691
|
+
</Menu.PrimaryItem>
|
|
692
|
+
<Menu.PrimaryItem
|
|
693
|
+
disabled
|
|
694
|
+
asChild
|
|
695
|
+
>
|
|
696
|
+
<a
|
|
697
|
+
href='/service'
|
|
698
|
+
tabIndex={-1}
|
|
699
|
+
>
|
|
700
|
+
Unavailable link
|
|
701
|
+
</a>
|
|
702
|
+
</Menu.PrimaryItem>
|
|
703
|
+
<Menu.PrimaryItem>Available</Menu.PrimaryItem>
|
|
704
|
+
</Menu>
|
|
705
|
+
);
|
|
706
|
+
|
|
707
|
+
expect(
|
|
708
|
+
screen.getByRole('button', { name: 'Unavailable action' })
|
|
709
|
+
).toHaveAttribute('tabindex', '-1');
|
|
710
|
+
expect(
|
|
711
|
+
screen.getByRole('link', { name: 'Unavailable link' })
|
|
712
|
+
).toHaveAttribute('tabindex', '-1');
|
|
713
|
+
await user.tab();
|
|
714
|
+
expect(screen.getByRole('button', { name: 'Available' })).toHaveFocus();
|
|
715
|
+
});
|
|
716
|
+
|
|
717
|
+
test('disabled accordion remains tabbable but cannot be expanded', async () => {
|
|
718
|
+
const user = userEvent.setup();
|
|
719
|
+
const onClick = vi.fn();
|
|
720
|
+
wrap(
|
|
721
|
+
<Menu>
|
|
722
|
+
<Menu.Accordion
|
|
723
|
+
label='Services'
|
|
724
|
+
disabled
|
|
725
|
+
onClick={onClick}
|
|
726
|
+
badge={<Badge variant='caution-subtle'>Unavailable</Badge>}
|
|
727
|
+
>
|
|
728
|
+
<Menu.Accordion.Item>Library</Menu.Accordion.Item>
|
|
729
|
+
</Menu.Accordion>
|
|
730
|
+
</Menu>
|
|
731
|
+
);
|
|
732
|
+
|
|
733
|
+
const trigger = screen.getByRole('button', {
|
|
734
|
+
name: 'Services Unavailable',
|
|
735
|
+
});
|
|
736
|
+
await user.tab();
|
|
737
|
+
expect(trigger).toHaveFocus();
|
|
738
|
+
await user.click(trigger);
|
|
739
|
+
await user.keyboard('{Enter} ');
|
|
740
|
+
expect(trigger).toHaveAttribute('aria-expanded', 'false');
|
|
741
|
+
expect(trigger).toHaveAttribute('aria-disabled', 'true');
|
|
742
|
+
expect(onClick).not.toHaveBeenCalled();
|
|
743
|
+
});
|
|
744
|
+
|
|
745
|
+
test.each(['light', 'dark'] as const)(
|
|
746
|
+
'uses %s disabled text tokens without dimming the badge',
|
|
747
|
+
(themeName) => {
|
|
748
|
+
const theme = themes[themeName];
|
|
749
|
+
render(
|
|
750
|
+
<ThemeContextProvider theme={themeName}>
|
|
751
|
+
<Menu>
|
|
752
|
+
<Menu.SecondaryItem
|
|
753
|
+
disabled
|
|
754
|
+
icon={<Icon.Home />}
|
|
755
|
+
badge={
|
|
756
|
+
<Badge
|
|
757
|
+
variant='caution-subtle'
|
|
758
|
+
icon
|
|
759
|
+
>
|
|
760
|
+
Temporary unavailable
|
|
761
|
+
</Badge>
|
|
762
|
+
}
|
|
763
|
+
>
|
|
764
|
+
Service
|
|
765
|
+
</Menu.SecondaryItem>
|
|
766
|
+
</Menu>
|
|
767
|
+
</ThemeContextProvider>
|
|
768
|
+
);
|
|
769
|
+
|
|
770
|
+
expect(screen.getByText('Service')).toHaveStyle({
|
|
771
|
+
color: theme.colour.text.disabled,
|
|
772
|
+
});
|
|
773
|
+
const badge = screen.getByTestId('ucl-uikit-badge');
|
|
774
|
+
expect(badge).toHaveStyle({
|
|
775
|
+
color: theme.colour.text.cautionSubtle,
|
|
776
|
+
backgroundColor: theme.colour.fill.cautionSubtle,
|
|
777
|
+
height: '22px',
|
|
778
|
+
});
|
|
779
|
+
expect(badge.querySelector('svg')).toHaveAttribute('width', '14');
|
|
780
|
+
}
|
|
781
|
+
);
|
|
782
|
+
|
|
783
|
+
test('collapsed disabled primary item hides the badge and stays tabbable', async () => {
|
|
784
|
+
const user = userEvent.setup();
|
|
785
|
+
wrap(
|
|
786
|
+
<Menu collapsed>
|
|
787
|
+
<Menu.PrimaryItem
|
|
788
|
+
disabled
|
|
789
|
+
active
|
|
790
|
+
icon={<Icon.Home />}
|
|
791
|
+
badge={<Badge variant='caution-subtle'>Unavailable</Badge>}
|
|
792
|
+
trailingContent='Extra'
|
|
793
|
+
asChild
|
|
794
|
+
>
|
|
795
|
+
<a href='/service'>Service</a>
|
|
796
|
+
</Menu.PrimaryItem>
|
|
797
|
+
</Menu>
|
|
798
|
+
);
|
|
799
|
+
|
|
800
|
+
const link = screen.getByRole('link', { name: 'Service' });
|
|
801
|
+
expect(link).toHaveAttribute('aria-disabled', 'true');
|
|
802
|
+
expect(link.tabIndex).toBe(0);
|
|
803
|
+
await user.tab();
|
|
804
|
+
expect(link).toHaveFocus();
|
|
805
|
+
expect(fireEvent.click(link)).toBe(false);
|
|
806
|
+
expect(screen.queryByTestId('ucl-uikit-badge')).not.toBeInTheDocument();
|
|
807
|
+
expect(screen.queryByText('Extra')).not.toBeInTheDocument();
|
|
808
|
+
expect(link.closest('.ucl-uikit-menu__primary-item')).toHaveStyle({
|
|
809
|
+
width: '32px',
|
|
810
|
+
color: themes.default.colour.text.disabled,
|
|
811
|
+
});
|
|
812
|
+
});
|
|
423
813
|
});
|
package/package.json
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
"name": "uikit-react-public",
|
|
3
3
|
"private": false,
|
|
4
4
|
"license": "UNLICENSED",
|
|
5
|
-
"version": "0.50.
|
|
5
|
+
"version": "0.50.13",
|
|
6
6
|
"type": "module",
|
|
7
7
|
"main": "dist/index.js",
|
|
8
8
|
"types": "dist/index.d.ts",
|
|
@@ -89,7 +89,7 @@
|
|
|
89
89
|
"react": "^19.1.0",
|
|
90
90
|
"react-dom": "^19.1.0",
|
|
91
91
|
"react-router": "^8.3.0",
|
|
92
|
-
"storybook": "10.
|
|
92
|
+
"storybook": "^10.6.3",
|
|
93
93
|
"typescript": "^5.8.2",
|
|
94
94
|
"typescript-eslint": "^8.26.1",
|
|
95
95
|
"vite": "^6.2.1",
|