Skip to content

Commit

Permalink
🧑‍💻SideSheet: open required, conditional render header, title, button (
Browse files Browse the repository at this point in the history
…#3161)

* 🧑‍💻SideSheet: open required, conditional render header, title, button

* Updated tests
  • Loading branch information
oddvernes authored Dec 4, 2023
1 parent 1f745ad commit 1904d9f
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,28 +11,32 @@ const StyledSidesheet = styled(SideSheet)`

describe('SideSheet', () => {
it('Matches snapshot', () => {
const { asFragment } = render(<SideSheet variant="large" title="Title" />)
const { asFragment } = render(
<SideSheet open variant="large" title="Title" onClose={jest.fn()} />,
)
expect(asFragment()).toMatchSnapshot()
})
it('Should pass a11y test', async () => {
const { container } = render(<SideSheet title="Title" />)
const { container } = render(<SideSheet open title="Title" />)
await act(async () => {
const results = await axe(container)
expect(results).toHaveNoViolations()
})
})
it('Has correct width', () => {
render(<SideSheet variant="large" title="Title" data-testid="sidesheet" />)
render(
<SideSheet open variant="large" title="Title" data-testid="sidesheet" />,
)
expect(screen.getByTestId('sidesheet')).toHaveStyleRule('width', '480px')
})
it('Has provided necessary props', () => {
const title = 'Title'
const variant = 'large'
render(<SideSheet variant={variant} title={title} />)
render(<SideSheet open variant={variant} title={title} />)
expect(screen.getByText(title)).toBeDefined()
})
it('Can extend the css for the component', () => {
render(<StyledSidesheet data-testid="sidesheet" />)
render(<StyledSidesheet open data-testid="sidesheet" />)
const sidesheet = screen.getByTestId('sidesheet')
expect(sidesheet).toHaveStyleRule('position', 'relative')
expect(sidesheet).toHaveStyleRule('height', '100px')
Expand Down
42 changes: 20 additions & 22 deletions packages/eds-core-react/src/components/SideSheet/SideSheet.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export type SideSheetProps = {
/** OnClick function (close) */
onClose?: (Event) => void
/** Open / close Side Sheet */
open?: boolean
open: boolean
/** Override width of Side Sheet */
width?: string
} & HTMLAttributes<HTMLDivElement>
Expand All @@ -52,19 +52,14 @@ const Header = styled.div`
align-items: center;
padding-bottom: 24px;
padding-right: 10px;
& > button {
margin-left: auto;
}
`

export const SideSheet = forwardRef<HTMLDivElement, SideSheetProps>(
function SideSheet(
{
variant = 'medium',
width,
title = '',
children,
open = true,
onClose,
...rest
},
{ variant = 'medium', width, title, children, open, onClose, ...rest },
ref,
) {
const props = {
Expand All @@ -73,21 +68,24 @@ export const SideSheet = forwardRef<HTMLDivElement, SideSheetProps>(
width: width || variants[variant],
}

// Controller must set open={false} when pressing the close button
return (
open && (
<StyledSideSheet {...props}>
<Header>
<Typography variant="h2">{title}</Typography>
<Button
variant="ghost_icon"
onClick={onClose}
title="Close"
aria-label="Close sidesheet"
>
<Icon name="clear" data={clear} />
</Button>
</Header>
{(title || onClose) && (
<Header>
{title && <Typography variant="h2">{title}</Typography>}
{onClose && (
<Button
variant="ghost_icon"
onClick={onClose}
title="Close"
aria-label="Close sidesheet"
>
<Icon name="clear" data={clear} />
</Button>
)}
</Header>
)}
{children}
</StyledSideSheet>
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,10 @@ exports[`SideSheet Matches snapshot 1`] = `
padding-right: 10px;
}
.c1>button {
margin-left: auto;
}
@media (hover:hover) and (pointer:fine) {
.c3:hover {
background: var(--eds_interactive_primary__hover_alt, rgba(222, 237, 238, 1));
Expand Down

0 comments on commit 1904d9f

Please sign in to comment.