Skip to content

Commit

Permalink
feat(Paragraph): handle nested paragraphs (to be span's)
Browse files Browse the repository at this point in the history
  • Loading branch information
tujoworker committed Nov 11, 2024
1 parent 505dbc1 commit b0a864d
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 13 deletions.
40 changes: 27 additions & 13 deletions packages/dnb-eufemia/src/elements/typography/P.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
*
*/

import React from 'react'
import React, { createContext, useContext } from 'react'
import classnames from 'classnames'
import { SpacingProps } from '../../components/space/types'
import type { DynamicElement } from '../../shared/types'
Expand Down Expand Up @@ -45,16 +45,19 @@ export type PProps = SpacingProps &
modifier?: string
}

const P = ({
modifier,
element = 'p',
className,
medium,
bold,
size,
...props
}: PProps) => {
function P(props: PProps) {
const {
modifier,
element = 'p',
className,
medium,
bold,
size,
...rest
} = props

const allModifiers = [medium && 'medium', bold && 'bold']
const paragraphContext = useContext(ParagraphContext)

if (modifier) {
modifier
Expand All @@ -74,18 +77,29 @@ const P = ({

return (
<E
as={element}
{...props}
as={element === 'p' && paragraphContext?.isNested ? 'span' : element}
{...rest}
className={classnames(
'dnb-p',
modifierString,
className,
size && `dnb-p__size--${size}`
)}
/>
>
<ParagraphContext.Provider value={{ isNested: true }}>
{props.children}
</ParagraphContext.Provider>
</E>
)
}

P._supportsSpacingProps = true

export default P

export type ParagraphContextType = {
isNested?: boolean
}

export const ParagraphContext =
createContext<ParagraphContextType>(undefined)
23 changes: 23 additions & 0 deletions packages/dnb-eufemia/src/elements/typography/__tests__/P.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,24 @@ const props: PProps = {
}

describe('P element', () => {
it('has p element as default', () => {
render(<P />)

const element = document.querySelector('.dnb-p')
expect(element.tagName).toBe('P')
})

it('has span element when nested', () => {
render(
<P>
<P />
</P>
)

const element = document.querySelector('.dnb-p > .dnb-p')
expect(element.tagName).toBe('SPAN')
})

it('has correct size when size is defined', () => {
render(<P size="large" />)
const element = document.querySelector('.dnb-p__size--large')
Expand All @@ -23,6 +41,7 @@ describe('P element', () => {
'dnb-p__size--large',
])
})

it('has correct style when size and a modifier is defined', () => {
render(<P size="medium" modifier="medium" />)
const element = document.querySelector('.dnb-p__size--medium')
Expand All @@ -33,6 +52,7 @@ describe('P element', () => {
'dnb-p__size--medium',
])
})

it('has correct style when several modifiers are defined', () => {
render(<P modifier="medium small" />)
const element = document.querySelector('.dnb-p__size--small')
Expand All @@ -43,6 +63,7 @@ describe('P element', () => {
'dnb-p__size--small',
])
})

it('has correct style when medium is set to true', () => {
render(<P medium />)
const element = document.querySelector('.dnb-p--medium')
Expand All @@ -51,12 +72,14 @@ describe('P element', () => {
'dnb-p--medium',
])
})

it('has correct style when bold is set to true', () => {
render(<P bold />)
const element = document.querySelector('.dnb-p--bold')

expect(Array.from(element.classList)).toEqual(['dnb-p', 'dnb-p--bold'])
})

it('should validate with ARIA rules as a p element', async () => {
const Comp = render(<P {...props} />)
expect(await axeComponent(Comp)).toHaveNoViolations()
Expand Down

0 comments on commit b0a864d

Please sign in to comment.