Skip to content

Commit

Permalink
fix(Dl): ensure correct spacing
Browse files Browse the repository at this point in the history
Using CSS gap did also include our "hidden" wrapper element. So we need to use margin instead.
  • Loading branch information
tujoworker committed Oct 12, 2022
1 parent 6845219 commit de602f1
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 25 deletions.
15 changes: 9 additions & 6 deletions packages/dnb-eufemia/src/elements/Dl.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import React from 'react'
import classnames from 'classnames'
import E, { ElementProps } from './Element'
import { SpacingProps } from '../shared/types'

export type DlProps = {
/**
Expand All @@ -28,18 +29,20 @@ const Dl = ({ direction, ...props }: DlAllProps) => {
return <E is="dl" {...props} skeleton={false} />
}

export type DlItemProps = {
//
}

Dl.Item = ({
className,
children,
}: DlItemProps & React.AllHTMLAttributes<HTMLSpanElement>) => {
...props
}: React.AllHTMLAttributes<HTMLSpanElement> & SpacingProps) => {
return (
<>
{children}
<dd aria-hidden className={classnames(className, 'dnb-dl__item')} />
<E
is="dd"
aria-hidden
className={classnames(className, 'dnb-dl__item')}
{...props}
/>
</>
)
}
Expand Down
3 changes: 2 additions & 1 deletion packages/dnb-eufemia/src/elements/Element.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
SkeletonMethods,
} from '../components/skeleton/SkeletonHelper'
import { includeValidProps } from '../components/form-row/FormRowHelpers'
import { SpacingProps } from '../shared/types'

export type ElementInternalProps = {
is: React.ReactNode
Expand All @@ -36,7 +37,7 @@ export type ElementProps = {

/** @deprecated use skeletonMethod instead */
skeleton_method?: SkeletonMethods
}
} & SpacingProps
type ElementAllProps = ElementInternalProps & ElementProps

type Attributes = Record<string, unknown>
Expand Down
44 changes: 44 additions & 0 deletions packages/dnb-eufemia/src/elements/__tests__/Lists.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,36 @@ import Dt from '../Dt'
import Dd from '../Dd'

describe('Dl', () => {
it('should support spacing props', () => {
render(
<Dl top="medium" direction="horizontal">
<Dl.Item top="medium">
<Dt top="medium">Term</Dt>
<Dd top="medium">Description</Dd>
</Dl.Item>
</Dl>
)

const element = document.querySelector('.dnb-dl')

expect(Array.from(element.classList)).toEqual([
'dnb-dl__direction--horizontal',
'dnb-space__top--medium',
'dnb-dl',
])
expect(Array.from(element.querySelector('dt').classList)).toEqual([
'dnb-space__top--medium',
'dnb-dt',
])
expect(Array.from(element.querySelector('dd').classList)).toEqual([
'dnb-space__top--medium',
'dnb-dd',
])
expect(
Array.from(element.querySelector('.dnb-dl__item').classList)
).toEqual(['dnb-dl__item', 'dnb-space__top--medium', 'dnb-dd'])
})

describe('in horizontal direction', () => {
it('should validate with ARIA rules', async () => {
const Component = render(
Expand All @@ -27,6 +57,20 @@ describe('Dl', () => {
)
expect(await axeComponent(Component)).toHaveNoViolations()
})

it('should have aria-hidden on item', async () => {
render(
<Dl direction="horizontal">
<Dl.Item>
<Dt>Term</Dt>
<Dd>Description</Dd>
</Dl.Item>
</Dl>
)
expect(
document.querySelector('.dnb-dl__item').getAttribute('aria-hidden')
).toBe('true')
})
})

describe('with nested Dls', () => {
Expand Down
12 changes: 7 additions & 5 deletions packages/dnb-eufemia/src/elements/stories/Lists.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export default {
}

export const ListSandbox = () => (
<Wrapper className="dnb-spacing">
<Wrapper>
<Provider>
<div>
<Box>
Expand Down Expand Up @@ -127,10 +127,12 @@ export const ListSandbox = () => (
<Dd>Description 1</Dd>
<Dd>Description 2</Dd>
<Dd>Description 3</Dd>
<dl className="dnb-dl">
<Dt>Sub Term</Dt>
<Dd>Sub Description</Dd>
</dl>
<Dd>
<Dl>
<Dt>Sub Term</Dt>
<Dd>Sub Description</Dd>
</Dl>
</Dd>
</Dl>
</Box>
<Box>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -733,22 +733,24 @@ a.dnb-button {
@media screen and (min-width: 40em) {
.dnb-dl__direction--horizontal {
display: flex;
flex-wrap: wrap;
gap: 0.5rem; }
flex-wrap: wrap; }
.dnb-dl__direction--horizontal dt {
margin-right: 0.5rem;
margin-right: 1rem;
max-width: 30%; }
.dnb-dl__direction--horizontal dd {
width: calc(70% - 1.5rem); }
width: calc(70% - 1rem); }
.dnb-dl__direction--horizontal dt,
.dnb-dl__direction--horizontal dd,
.dnb-dl__direction--horizontal dd ~ dt {
margin-top: 0;
margin-top: 0.5rem;
margin-bottom: 0; }
.dnb-dl__direction--horizontal dt:first-of-type,
.dnb-dl__direction--horizontal dd:first-of-type,
.dnb-dl__direction--horizontal dd ~ dt:first-of-type {
margin-top: 0; }
.dnb-dl dd.dnb-dl__item {
width: 100%;
height: 0;
overflow: hidden; } }
height: 0; } }
.dnb-spacing .dnb-ul:not([class*='dnb-space__top']),
.dnb-spacing .dnb-ol:not([class*='dnb-space__top']) {
Expand Down
15 changes: 9 additions & 6 deletions packages/dnb-eufemia/src/style/elements/lists.scss
Original file line number Diff line number Diff line change
Expand Up @@ -139,30 +139,33 @@
// Inline
@include allAbove(small) {
&__direction--horizontal {
// We can not use gap: 0.5rem because of our wrapper "__item"
display: flex;
flex-wrap: wrap;
gap: 0.5rem;

& dt {
margin-right: 0.5rem;
margin-right: 1rem;
max-width: 30%;
}

& dd {
// width: calc(70% - 1.5rem); // minus margin-right and the gap
width: calc(70% - 1.5rem); // minus margin-right and the gap
width: calc(70% - 1rem); // minus margin-right and the gap
}

& dt,
& dd,
& dd ~ dt {
margin-top: 0;
margin-top: 0.5rem;
margin-bottom: 0;
&:first-of-type {
margin-top: 0;
}
}
}

dd#{&}__item {
width: 100%;
height: 0;
overflow: hidden;
}
}
}
Expand Down

0 comments on commit de602f1

Please sign in to comment.