Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: Introduce UNSTABLE Header & Navigation #1808

Draft
wants to merge 23 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
e520c40
Feat(web): Introduce UNSTABLE Header component #DS-1523
crishpeen Dec 3, 2024
e8a14b0
Feat(web): Introduce Navigation and NavigationLink components #DS-155…
crishpeen Dec 3, 2024
6e495df
Feat(web-react): Introduce UNSTABLE_Header #DS-1524
curdaj Dec 2, 2024
be9db49
Feat(web-react): Introduce Navigation #DS-1524
curdaj Dec 2, 2024
2be475f
Fix(web): Rename `NavigationLink` component to `NavigationAction`
crishpeen Dec 17, 2024
7e558cb
Fix(web-react): Rename `NavigationLink` component to `NavigationAction`
crishpeen Dec 17, 2024
e15404f
Test: Update Homepage Screenshots
crishpeen Jan 8, 2025
433795c
Feat(web-react): Introduce Drawer component #DS-1580
curdaj Dec 30, 2024
63c47ee
Feat(web): Introduce Drawer component #DS-1580
curdaj Dec 30, 2024
e289c4a
Feat(web): Introduce `alignment` to `Dropdown` component #DS-1411
crishpeen Jan 8, 2025
9a5c06c
Feat(web-react): Introduce `alignment` to `Dropdown` component #DS-1411
crishpeen Jan 8, 2025
0c4e5e7
Feat(web-twig): Introduce `alignment` to `Dropdown` component #DS-1411
crishpeen Jan 8, 2025
f619d4c
Feat(web): Introduce `button-unstyled` helper
crishpeen Dec 17, 2024
9507130
Feat(web): Introduce `NavigationItem` subcomponent #DS-1411
crishpeen Jan 8, 2025
5afb856
Feat(web-react): Introduce `NavigationItem` subcomponent #DS-1411
crishpeen Jan 8, 2025
b3c98d1
Docs(web): Showcase Header with Navigation and Dropdown #DS-1411
crishpeen Jan 8, 2025
8fe93f9
Docs(web-react): Showcase Header with Navigation and Dropdown #DS-1411
crishpeen Jan 8, 2025
6b31529
Feat(web): Refactor Accessibility expanded helpers to prevent wrong i…
crishpeen Jan 21, 2025
b0623f3
Docs(web): Tidy up Drawer demo
crishpeen Jan 21, 2025
9b88827
Feat(web): Introduce Vertical Navigation #DS-1627
crishpeen Jan 21, 2025
e5988de
Feat(web): Add more complex Header examples #DS-1627
crishpeen Jan 21, 2025
0e5883b
Feat(web-react): Introduce Vertical Navigation #DS-1627
curdaj Jan 22, 2025
6e23b99
Feat(web-react): Add Drawer with Navigation to Header demo #DS-1627
curdaj Jan 23, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions packages/web-react/scripts/entryPoints.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ const entryPoints = [
{ dirs: ['components', 'Container'] },
{ dirs: ['components', 'Dialog'] },
{ dirs: ['components', 'Divider'] },
{ dirs: ['components', 'Drawer'] },
{ dirs: ['components', 'Dropdown'] },
{ dirs: ['components', 'Field'] },
{ dirs: ['components', 'FieldGroup'] },
Expand All @@ -35,6 +36,7 @@ const entryPoints = [
{ dirs: ['components', 'Item'] },
{ dirs: ['components', 'Link'] },
{ dirs: ['components', 'Modal'] },
{ dirs: ['components', 'Navigation'] },
{ dirs: ['components', 'NoSsr'] },
{ dirs: ['components', 'Pagination'] },
{ dirs: ['components', 'PartnerLogo'] },
Expand All @@ -56,6 +58,7 @@ const entryPoints = [
{ dirs: ['components', 'UNSTABLE_ActionLayout'] },
{ dirs: ['components', 'UNSTABLE_Avatar'] },
{ dirs: ['components', 'UNSTABLE_EmptyState'] },
{ dirs: ['components', 'UNSTABLE_Header'] },
{ dirs: ['components', 'UNSTABLE_Slider'] },
{ dirs: ['components', 'UNSTABLE_Toggle'] },
{ dirs: ['components', 'UNSTABLE_Truncate'] },
Expand Down
41 changes: 41 additions & 0 deletions packages/web-react/src/components/Drawer/Drawer.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
'use client';

import classNames from 'classnames';
import React from 'react';
import { useStyleProps, useLastActiveFocus } from '../../hooks';
import { SpiritDrawerProps } from '../../types';
import { Dialog } from '../Dialog';
import { DRAWER_ALIGNMENT_DEFAULT } from './constants';
import { DrawerProvider } from './DrawerContext';
import { useDrawerStyleProps } from './useDrawerStyleProps';

const Drawer = (props: SpiritDrawerProps) => {
const { children, alignmentX = DRAWER_ALIGNMENT_DEFAULT, isOpen, onClose, id, ...restProps } = props;
const { classProps } = useDrawerStyleProps({ drawerAlignmentX: alignmentX });
const { styleProps, props: otherProps } = useStyleProps(restProps);

const contextValue = {
id,
isOpen,
onClose,
};

useLastActiveFocus(isOpen);

return (
<DrawerProvider value={contextValue}>
<Dialog
{...otherProps}
{...styleProps}
id={id}
isOpen={isOpen}
onClose={onClose}
className={classNames(classProps.root, styleProps.className)}
>
{children}
</Dialog>
</DrawerProvider>
);
};

export default Drawer;
38 changes: 38 additions & 0 deletions packages/web-react/src/components/Drawer/DrawerCloseButton.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
'use client';

import classNames from 'classnames';
import React from 'react';
import { useStyleProps } from '../../hooks';
import { DrawerCloseButtonProps } from '../../types';
import { Button } from '../Button';
import { Icon } from '../Icon';
import { VisuallyHidden } from '../VisuallyHidden';
import { DRAWER_CLOSE_BUTTON_LABEL_DEFAULT } from './constants';
import { useDrawerContext } from './DrawerContext';
import { useDrawerStyleProps } from './useDrawerStyleProps';

const DrawerCloseButton = (props: DrawerCloseButtonProps) => {
const { label = DRAWER_CLOSE_BUTTON_LABEL_DEFAULT, ...restProps } = props;
const { id, isOpen, onClose } = useDrawerContext();

const { classProps } = useDrawerStyleProps();
const { styleProps, props: otherProps } = useStyleProps(restProps);

return (
<Button
{...otherProps}
aria-expanded={isOpen}
aria-controls={id}
onClick={onClose}
color="tertiary"
UNSAFE_className={classNames(classProps.closeButton, styleProps.className)}
UNSAFE_style={styleProps.style}
isSymmetrical
>
<Icon name="close" />
<VisuallyHidden>{label}</VisuallyHidden>
</Button>
);
};

export default DrawerCloseButton;
22 changes: 22 additions & 0 deletions packages/web-react/src/components/Drawer/DrawerContext.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
'use client';

import { createContext, useContext } from 'react';
import { DrawerPanelHandlingProps } from '../../types';

export type DrawerContextProps = {
id: string;
} & DrawerPanelHandlingProps;

const defaultContext: DrawerContextProps = {
id: '',
isOpen: false,
onClose: () => null,
};

const DrawerContext = createContext<DrawerContextProps>(defaultContext);
const DrawerProvider = DrawerContext.Provider;
const DrawerConsumer = DrawerContext.Consumer;
const useDrawerContext = (): DrawerContextProps => useContext(DrawerContext);

export default DrawerContext;
export { DrawerProvider, DrawerConsumer, useDrawerContext };
34 changes: 34 additions & 0 deletions packages/web-react/src/components/Drawer/DrawerPanel.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
'use client';

import classNames from 'classnames';
import React, { ElementType, ForwardedRef, forwardRef, HTMLAttributes } from 'react';
import { useStyleProps } from '../../hooks';
import { DrawerPanelElementType, DrawerPanelProps } from '../../types';
import { useDrawerStyleProps } from './useDrawerStyleProps';

/* We need an exception for components exported with forwardRef */
/* eslint no-underscore-dangle: ['error', { allow: ['_DrawerPanel'] }] */
const _DrawerPanel = <E extends ElementType = DrawerPanelElementType>(
props: DrawerPanelProps<E>,
ref: ForwardedRef<HTMLDivElement>,
) => {
const { elementType: ElementTag = 'div', children, ...restProps } = props;

const { classProps } = useDrawerStyleProps(restProps);
const { styleProps, props: otherProps } = useStyleProps(restProps);

return (
<ElementTag
ref={ref}
{...(otherProps as HTMLAttributes<HTMLElement>)}
className={classNames(classProps.panel, styleProps.className)}
style={styleProps.style}
>
<div className={classProps.content}>{children}</div>
</ElementTag>
);
};

const DrawerPanel = forwardRef<HTMLDivElement, DrawerPanelProps<ElementType>>(_DrawerPanel);

export default DrawerPanel;
145 changes: 145 additions & 0 deletions packages/web-react/src/components/Drawer/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
# Drawer

The Drawer component is a container that slides in from side of the screen. It can be used to display additional content or actions that are not part of the main view.

The Drawer is a composition of several subcomponents:

- [Drawer](#drawer)
- [DrawerCloseButton](#drawerclosebutton)
- [DrawerPanel](#drawerpanel)

## Accessibility Guidelines

👉 The animation effect of this component is dependent on the
`prefers-reduced-motion` media query.

## Drawer

```jsx
import { Drawer } from '@lmc-eu/spirit-web-react';

const [isOpen, setOpen] = useState(false);

<Drawer id="drawer-dialog-example" isOpen={isOpen} onClose={() => setOpen(false)}>
{/* Drawer Panel goes here */}
</Drawer>;
```

### Alignment

The `Drawer` component allows aligning the content panel horizontally to the left or right side of the screen using `alignmentX` prop. By default, the drawer content panel is aligned to the right.

```jsx
<Drawer id="drawer-dialog-example" isOpen={isOpen} onClose={() => setOpen(false)} alignmentX="left">
{/* Drawer Panel goes here */}
</Drawer>
```

### Close on Backdrop Click

By default, the drawer will close when the backdrop is clicked. You can disable this behavior by setting the `closeOnBackdropClick` prop to `false`.

```jsx
<Drawer id="drawer-dialog-example" isOpen={isOpen} onClose={() => setOpen(false)} closeOnBackdropClick={false}>
{/* Drawer content goes here */}
</Drawer>
```

### Close on Escape Key Down

By default, the drawer will close when the escape key is pressed. You can disable this behavior by setting the `closeOnEscapeKeyDown` prop to `false`.

```jsx
<Drawer id="drawer-dialog-example" isOpen={isOpen} onClose={() => setOpen(false)} closeOnEscapeKeyDown={false}>
{/* Drawer content goes here */}
</Drawer>
```

### API

| Name | Type | Default | Required | Description |
| ---------------------- | ---------------------------------------------- | ------- | -------- | -------------------------------------------------------- |
| `alignmentX` | `left` \| `right` | `right` | ✕ | Drawer horizontal alignment |
| `children` | `ReactNode` | — | ✕ | Children node |
| `closeOnBackdropClick` | `bool` | `true` | ✕ | Whether the drawer will close when backdrop is clicked |
| `closeOnEscapeKeyDown` | `bool` | `true` | ✕ | Whether the drawer will close when escape key is pressed |
| `id` | `string` | — | ✓ | ID to be linked |
| `isOpen` | `bool` | `false` | ✓ | Open state |
| `onClose` | `(event: ClickEvent or KeyboardEvent) => void` | — | ✓ | Callback for drawer when closed |

The component further inherits properties from the [`<dialog>`][mdn-dialog-element] element.

On top of the API options, the components accept [additional attributes][readme-additional-attributes].
If you need more control over the styling of a component, you can use [style props][readme-style-props]
and [escape hatches][readme-escape-hatches].

## DrawerCloseButton

The `DrawerCloseButton` component is a button that closes the drawer when clicked.

```jsx
import { DrawerCloseButton } from '@lmc-eu/spirit-web-react';

<DrawerCloseButton />;
```

### API

| Name | Type | Default | Required | Description |
| ------- | -------- | ------- | -------- | -------------------------------- |
| `label` | `string` | `Close` | ✕ | Label of the drawer close button |

The component further inherits properties from the [`<button>`][mdn-button-element] element.

On top of the API options, the components accept [additional attributes][readme-additional-attributes].
If you need more control over the styling of a component, you can use [style props][readme-style-props]
and [escape hatches][readme-escape-hatches].

## DrawerPanel

The `DrawerPanel` component is a container for the content that will be displayed in the drawer.

```jsx
import { DrawerPanel } from '@lmc-eu/spirit-web-react';

<DrawerPanel>{/* Drawer content goes here */}</DrawerPanel>;
```

### API

| Name | Type | Default | Required | Description |
| ------------- | ------------- | ------- | -------- | ------------------------------------ |
| `children` | `ReactNode` | — | ✕ | Children node |
| `elementType` | `ElementType` | `div` | ✕ | Type of element used as drawer panel |

## Full Example

```jsx
import { Drawer, DrawerPanel, DrawerCloseButton } from '@lmc-eu/spirit-web-react';

const [isOpen, setIsOpen] = useState(false);

const handleOpen = () => setIsOpen(true);
const handleClose = () => setIsOpen(false);

<Button onClick={handleOpen} aria-controls="drawer-example">
Open Drawer
</Button>

<Drawer
id="drawer-example"
isOpen={isOpen}
onClose={handleClose}
>
<DrawerPanel>
<DrawerCloseButton />
<div>Drawer content</div>
</DrawerPanel>
</Drawer>
```

[mdn-button-element]: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/button
[mdn-dialog-element]: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/dialog
[readme-additional-attributes]: https://github.com/lmc-eu/spirit-design-system/blob/main/packages/web-react/README.md#additional-attributes
[readme-escape-hatches]: https://github.com/lmc-eu/spirit-design-system/blob/main/packages/web-react/README.md#escape-hatches
[readme-style-props]: https://github.com/lmc-eu/spirit-design-system/blob/main/packages/web-react/README.md#style-props
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import '@testing-library/jest-dom';
import { fireEvent, render, screen } from '@testing-library/react';
import React from 'react';
import { classNamePrefixProviderTest, restPropsTest, stylePropsTest } from '@local/tests';
import { SpiritDrawerProps } from '../../../types';
import Drawer from '../Drawer';

const mockedOnClose = jest.fn();

describe('Drawer', () => {
const DrawerTest = (props: SpiritDrawerProps) => (
<Drawer {...props} id="drawer-example" isOpen={false} onClose={() => null}>
<div>Test</div>
</Drawer>
);

classNamePrefixProviderTest(DrawerTest, 'Drawer');

stylePropsTest(DrawerTest);

restPropsTest(DrawerTest, 'dialog');

it('should not close drawer', () => {
render(
<Drawer id="test" isOpen onClose={mockedOnClose} closeOnBackdropClick={false}>
<div>Test</div>
</Drawer>,
);

const dialog = screen.getByRole('dialog');
fireEvent.click(dialog);

expect(mockedOnClose).not.toHaveBeenCalled();
});

it('should close drawer', () => {
render(
<Drawer id="test" isOpen onClose={mockedOnClose} closeOnBackdropClick>
<div>Test</div>
</Drawer>,
);

const dialog = screen.getByRole('dialog');
fireEvent.click(dialog);

expect(mockedOnClose).toHaveBeenCalled();
});

it('should render drawer content', () => {
render(
<Drawer id="test" isOpen onClose={mockedOnClose}>
<div>Test</div>
</Drawer>,
);

expect(screen.getByRole('dialog')).toHaveTextContent('Test');
});

it('should render drawer with correct alignment class', () => {
render(
<Drawer id="test" isOpen onClose={mockedOnClose} alignmentX="left">
<div>Test</div>
</Drawer>,
);

expect(screen.getByRole('dialog')).toHaveClass('Drawer--left');
});
});
Loading
Loading