Skip to content

Commit

Permalink
fix(Accordion): support nested accordions
Browse files Browse the repository at this point in the history
  • Loading branch information
tujoworker committed Oct 3, 2022
1 parent 0560ce3 commit 45da964
Show file tree
Hide file tree
Showing 8 changed files with 235 additions and 136 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -266,3 +266,37 @@ export const AccordionPlainVariant = () => {
</ComponentBox>
) : null
}

export const AccordionNestedExample = () => {
return (
<ComponentBox data-visual-test="accordion-nested">
{
/* jsx */ `
<Accordion
id="nested-accordion"
title="Accordion"
expanded
space
>
<P space={false}>Content A</P>
<Accordion
id="nested-accordion-1"
title="Accordion nested 1"
space
>
<P space={false}>I'm nested 1</P>
</Accordion>
<P space={false}>Content B</P>
<Accordion
id="nested-accordion-2"
title="Accordion nested 2"
space
>
<P space={false}>I'm nested 2</P>
</Accordion>
</Accordion>
`
}
</ComponentBox>
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ AccordionLargeContentExample,
AccordionCustomisationExample,
AccordionContainerExample,
AccordionGroupExample,
AccordionPlainVariant
AccordionNestedExample,
AccordionPlainVariant,
} from 'Docs/uilib/components/accordion/Examples'

## Demos
Expand All @@ -31,6 +32,10 @@ AccordionPlainVariant

<AccordionCustomisationExample />

### Nested Accordions

<AccordionNestedExample />

### Accordion with a single container

A single container is only used for wider screens (desktop). When the users' screen is narrower (mobile), it will change to a normal accordion. The change happens with CSS only, so it will not interrupt any React render.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,15 @@ describe('Accordion closed screenshot', () => {
})
expect(screenshot).toMatchImageSnapshot()
})

it('have to match accordion in nested accordions', async () => {
const screenshot = await testPageScreenshot({
style: { width: '20rem', height: '25rem' },
selector: '[data-visual-test="accordion-nested"]',
simulateSelector: '[data-visual-test="accordion-nested"]',
})
expect(screenshot).toMatchImageSnapshot()
})
})

describe('Accordion container screenshot', () => {
Expand Down

Large diffs are not rendered by default.

Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,46 @@ const DidRender = ({ message }) => {
return <></>
}

export const NestedAccordion = () => {
return (
<Wrapper>
<Box>
<Accordion
id="nested-accordion"
title="Accordion"
expanded
// remember_state
space
prevent_rerender
>
<P>Text A</P>
<Accordion
id="nested-accordion-1"
title="Accordion 1"
expanded
// remember_state
space
prevent_rerender
>
<P>I'm nested 1</P>
</Accordion>
<P>Text B</P>
<Accordion
id="nested-accordion-2"
title="Accordion 2 very long title"
expanded
// remember_state
space
prevent_rerender
>
<P>I'm nested 2</P>
</Accordion>
</Accordion>
</Box>
</Wrapper>
)
}

export const AccordionSandbox = () => {
return (
<Wrapper>
Expand Down Expand Up @@ -324,7 +364,7 @@ function AccordionWithContainer() {
}
function ChangingContent({ changeHeight, children }) {
const [contentSize, changeContentSize] = React.useState(false)
React.useEffect(() => {
React.useLayoutEffect(() => {
changeHeight.current.setContainerHeight()
}, [changeHeight, contentSize])
return (
Expand All @@ -339,6 +379,8 @@ function ChangingContent({ changeHeight, children }) {
Toggle content size
</ToggleButton>

{String(contentSize)}

{contentSize ? children : null}
</>
)
Expand Down
29 changes: 14 additions & 15 deletions packages/dnb-eufemia/src/components/accordion/style/_accordion.scss
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@
cursor: pointer;
outline: none;

border: var(--accordion-border-width) solid transparent;
border-radius: var(--accordion-border-radius);

&:focus {
user-select: none;
}
Expand All @@ -40,11 +43,6 @@
pointer-events: none;
}

border: var(--accordion-border-width) solid transparent;
border-radius: var(--accordion-border-radius);

// background-color: var(--color-white);

&__wrapper {
display: flex;
flex-direction: column;
Expand Down Expand Up @@ -116,23 +114,22 @@
}
}

&--expanded &__header {
&__icon {
transform: rotate(-180deg);
}
}

&--no-animation &__header,
html[data-visual-test] &__header {
&__icon {
transition-duration: 1ms !important;
}
}

&--expanded > &__header &__header__icon {
transform: rotate(-180deg);
}

&__content {
display: flex;
opacity: 1;
visibility: visible;
width: 100%;

will-change: height;
transition: height 400ms var(--accordion-easing),
Expand All @@ -149,6 +146,7 @@
}

&__inner {
width: 100%;
margin-top: 1rem;

&--remove-content {
Expand All @@ -168,10 +166,6 @@
@media screen and (min-width: 40em) {
&__header {
width: 40%; // 40% / 60%

&__icon {
transform: rotate(-90deg);
}
}

// Only to make sure we have a spacing. But would be typcally project spesific
Expand All @@ -194,6 +188,11 @@
// }
// }
}
&-group--single-container & > &__header &__header__icon {
@media screen and (min-width: 40em) {
transform: rotate(-90deg);
}
}
&--no-animation &__content,
html[data-visual-test] &__content {
transition-duration: 1ms !important;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,28 +9,25 @@
.dnb-accordion {
&__header {
border: none;
}
> &__header &__header__icon,
> &__header &__header__container {
color: var(--color-emerald-green);
}

&__icon,
&__container {
color: var(--color-emerald-green);
}

&__description {
color: var(--color-black-55);
}
> &__header &__header__description {
color: var(--color-black-55);
}

&--expanded &__header {
&__title {
font-weight: var(--font-weight-medium);
}
&--expanded > &__header &__header__title {
font-weight: var(--font-weight-medium);
}

// &__variant--default {
// }

// Variant: outlined
&__variant--outlined &__header {
&__variant--outlined > &__header {
color: var(--color-black-80);
background-color: var(--color-white);

Expand All @@ -44,49 +41,52 @@

@include fakeBorder(var(--color-emerald-green), 0.0625rem);
}

&[disabled] {
background-color: var(--color-white);
@include fakeBorder(var(--color-sea-green-30), 0.0625rem);
* {
color: var(--color-sea-green-30);
}
}
&__variant--outlined > &__header &__header[disabled] {
background-color: var(--color-white);
@include fakeBorder(var(--color-sea-green-30), 0.0625rem);
* {
color: var(--color-sea-green-30);
}
}
&__variant--outlined#{&}--expanded &__header {
&__variant--outlined#{&}--expanded > &__header {
color: var(--color-white);
background-color: var(--color-sea-green);

@include fakeBorder(transparent);

&[disabled] {
background-color: var(--color-sea-green-30);
* {
color: var(--color-white);
}
}
&__variant--outlined#{&}--expanded > &__header &__header[disabled] {
background-color: var(--color-sea-green-30);
* {
color: var(--color-white);
}
}

// Additional focus handling
// Additional focus handling
&__variant--outlined#{&}--expanded
> &__header
html[data-whatinput='keyboard']
&:not([disabled]):not(:active):not(:hover):focus {
background-color: var(--color-mint-green);
* {
color: var(--color-emerald-green);
}
}
&:not([disabled]):not(:active):not(:hover):focus {
@include fakeFocus(null, var(--color-emerald-green), inset);
&__header:not([disabled]):not(:active):not(:hover):focus {
background-color: var(--color-mint-green);
* {
color: var(--color-emerald-green);
}
}
&__variant--outlined#{&}--expanded
> &__header
&__header:not([disabled]):not(:active):not(:hover):focus {
@include fakeFocus(null, var(--color-emerald-green), inset);
}

&__variant--outlined#{&}--expanded
> &__header
&__header:not(:hover):not([disabled]) {
* {
color: var(--color-white);
}
}
// we have this hover variant, because of the default html focus/hover handling
&__variant--outlined#{&}--expanded &__header#{&}--hover:not(:active) {
&__variant--outlined#{&}--expanded > &__header#{&}--hover:not(:active) {
@include hover {
color: var(--color-white);
background-color: var(--color-sea-green);
Expand Down

0 comments on commit 45da964

Please sign in to comment.