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(Details): Add summary subcomponent #5015

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
f334af7
fix(Details): Add summary prop and subcomponent
francinelucca Sep 24, 2024
cbae243
Create old-boxes-clap.md
francinelucca Sep 24, 2024
c223dbb
fix: lint
francinelucca Sep 24, 2024
df23a60
fix(Docs): details story use default export
francinelucca Sep 24, 2024
3f449b5
Merge branch 'main' of github.com:primer/react into francinelucca/fix…
francinelucca Oct 4, 2024
bac525a
fix(Details): remove summary prop in favor of Details.Summary
francinelucca Oct 4, 2024
1eac3fb
test(Details): fix breaking tests
francinelucca Oct 4, 2024
3421e39
update changeset
francinelucca Oct 8, 2024
2d3b1b8
fix(Summary): add polymorphic type
francinelucca Oct 8, 2024
c719ca3
docs(Defatils): revert default story changes
francinelucca Oct 8, 2024
03a43f6
Merge branch 'main' into francinelucca/fix/3518-prcdetails-component-…
francinelucca Oct 8, 2024
70379c6
set output to true instead of files
siddharthkp Oct 8, 2024
16f1783
Merge branch 'workflow/fix-broken-recommend' into francinelucca/fix/3…
siddharthkp Oct 8, 2024
5608137
Merge branch 'main' into francinelucca/fix/3518-prcdetails-component-…
francinelucca Oct 10, 2024
23ebc11
Update .changeset/old-boxes-clap.md
francinelucca Oct 10, 2024
773392f
Merge branch 'main' into francinelucca/fix/3518-prcdetails-component-…
francinelucca Oct 10, 2024
28cfdc3
Merge branch 'main' of github.com:primer/react into francinelucca/fix…
francinelucca Oct 17, 2024
aed3b32
fix(Details): refactor summary detection logic
francinelucca Oct 18, 2024
75e505e
fix(Details): remove console log
francinelucca Oct 18, 2024
767a70a
test(Details): remove unused import
francinelucca Oct 18, 2024
7885485
Merge branch 'main' into francinelucca/fix/3518-prcdetails-component-…
francinelucca Oct 18, 2024
a8b604e
Merge branch 'main' of github.com:primer/react into francinelucca/fix…
francinelucca Oct 21, 2024
e983199
fix(Details): use mutationObserver to track child changes
francinelucca Oct 21, 2024
6ad6f48
test(Details): fix async tests
francinelucca Oct 21, 2024
b9dbc27
fix(Details): remove summary warning
francinelucca Oct 23, 2024
b288bc6
Merge branch 'main' of github.com:primer/react into francinelucca/fix…
francinelucca Oct 23, 2024
1f33300
Merge branch 'main' of github.com:primer/react into francinelucca/fix…
francinelucca Oct 24, 2024
a86ae4d
fix(Summary): do not forward a ref
francinelucca Oct 24, 2024
5c99c45
fix(Summary): do not use sx prop
francinelucca Oct 24, 2024
88c284e
fix(Summary): fix types
francinelucca Oct 24, 2024
e6ce3b3
Merge branch 'main' into francinelucca/fix/3518-prcdetails-component-…
francinelucca Oct 31, 2024
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
5 changes: 5 additions & 0 deletions .changeset/old-boxes-clap.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@primer/react": minor
---

feat(Details): Add summary subcomponent
23 changes: 22 additions & 1 deletion packages/react/src/Details/Details.docs.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,26 @@
"type": "SystemStyleObject"
}
],
"subcomponents": []
"subcomponents": [
{
"name": "Details.Summary",
"props": [
{
"name": "as",
"type": "React.ElementType<React.PropsWithChildren<any>>",
"defaultValue": "summary",
"required": false,
"description": "HTML element to render summary as."
},
{
"name": "children",
"type": "React.ReactNode"
},
{
"name": "sx",
"type": "SystemStyleObject"
}
]
}
]
Copy link
Member

@siddharthkp siddharthkp Oct 2, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(blocking) summary prop is missing from docs.

Curious, should we prefer adding just 1 approach instead of both <Details summary> and <Details.Summary>? To me, <Details.Summary> feels like the closest replacement for <summary>, which feels complete.

Would like to hear your thoughts on the 2 approaches though!

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thought about this and I agree with you. I think originally I was going for the "maximize ease-of-use" approach but in hindsight this might be confusing and adds extra complexity with having to support the intersection of both strategies (Re: your comment here).

Going to refactor to just introduce Details.Summary and render a default one if one isn't present, that feels right-er to me as well.
Wondering, do you think that would warrant a warning as well? (The use case where a Details component is used but neither Details.Summary nor a native summary is present within the children).

}
2 changes: 1 addition & 1 deletion packages/react/src/Details/Details.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export const Default: StoryFn<typeof Details> = () => {
const {getDetailsProps} = useDetails({closeOnOutsideClick: true})
return (
<Details {...getDetailsProps()}>
<Button as="summary">See Details</Button>
<Details.Summary as={Button}>See Details</Details.Summary>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Couple noob questions here:

  1. Assuming this renders a <button>, is that still accessible with <details>?
  2. Just checking, does this also support ButtonProps like size=small and variant=primary`?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. Note sure what accessible with details means 😅, here's how/what it renders:
image is there some specific behavior you want me to test for here?
  1. Had to fiddle with the types a little but yeah those work:
image image

This is some content
</Details>
)
Expand Down
67 changes: 63 additions & 4 deletions packages/react/src/Details/Details.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import React, {type ComponentPropsWithoutRef, type ReactElement} from 'react'
import React, {useEffect, useState, type ComponentPropsWithoutRef, type ReactElement} from 'react'
import styled from 'styled-components'
import type {SxProp} from '../sx'
import sx from '../sx'
import {toggleStyledComponent} from '../internal/utils/toggleStyledComponent'
import {useFeatureFlag} from '../FeatureFlags'
import {clsx} from 'clsx'
import classes from './Details.module.css'
import {useMergedRefs} from '../internal/hooks/useMergedRefs'

const CSS_MODULES_FEATURE_FLAG = 'primer_react_css_modules_team'

Expand All @@ -24,18 +25,76 @@ const StyledDetails = toggleStyledComponent(
`,
)

const Details = React.forwardRef<HTMLDetailsElement, DetailsProps>(
({className, children, ...rest}, ref): ReactElement => {
const Root = React.forwardRef<HTMLDetailsElement, DetailsProps>(
({className, children, ...rest}, forwardRef): ReactElement => {
const enabled = useFeatureFlag(CSS_MODULES_FEATURE_FLAG)
const detailsRef = React.useRef<HTMLDetailsElement>(null)
const ref = useMergedRefs(forwardRef, detailsRef)
const [hasSummary, setHasSummary] = useState(false)

useEffect(() => {
const {current: details} = detailsRef
if (!details) {
return
}

const updateSummary = () => {
const summary = details.querySelector('summary:not([data-default-summary])')
setHasSummary(!!summary)
}

// Update summary on mount
updateSummary()

const observer = new MutationObserver(() => {
updateSummary()
})

observer.observe(details, {
childList: true,
subtree: true,
})

return () => {
observer.disconnect()
}
}, [])

return (
<StyledDetails className={clsx(className, {[classes.Details]: enabled})} {...rest} ref={ref}>
{/* Include default summary if summary is not provided */}
{!hasSummary && <Details.Summary data-default-summary>{'See Details'}</Details.Summary>}
{children}
</StyledDetails>
)
},
)

Details.displayName = 'Details'
Root.displayName = 'Details'

export type SummaryProps<As extends React.ElementType> = {
/**
* HTML element to render summary as.
*/
as?: As
children?: React.ReactNode
} & React.ComponentPropsWithoutRef<React.ElementType extends As ? As : 'summary'>

function Summary<As extends React.ElementType>({as, children, ...props}: SummaryProps<As>) {
const Component = as ?? 'summary'
return (
<Component as={Component === 'summary' ? null : 'summary'} {...props}>
{children}
</Component>
)
}
Summary.displayName = 'Summary'

export {Summary}

const Details = Object.assign(Root, {
Summary,
})

export type DetailsProps = ComponentPropsWithoutRef<'details'> & SxProp
export default Details
80 changes: 68 additions & 12 deletions packages/react/src/Details/__tests__/Details.test.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {render} from '@testing-library/react'
import {render, screen} from '@testing-library/react'
import userEvent from '@testing-library/user-event'
import React from 'react'
import React, {act} from 'react'
import {Details, useDetails, Box, Button} from '../..'
import type {ButtonProps} from '../../Button'
import {behavesAsComponent, checkExports} from '../../utils/testing'
Expand All @@ -14,29 +14,36 @@ describe('Details', () => {
})

it('should have no axe violations', async () => {
const {container} = render(<Details />)
const results = await axe.run(container)
const {container} = render(
<Details>
<Details.Summary>Summary</Details.Summary>Content
</Details>,
)
let results
await act(async () => {
results = await axe.run(container)
})
expect(results).toHaveNoViolations()
})

it('Toggles when you click outside', () => {
it('Toggles when you click outside', async () => {
const Component = () => {
const {getDetailsProps} = useDetails({closeOnOutsideClick: true})
return (
<Details data-testid="details" {...getDetailsProps()}>
<summary>hi</summary>
<Details.Summary>hi</Details.Summary>
</Details>
)
}

const {getByTestId} = render(<Component />)
const {findByTestId} = render(<Component />)

document.body.click()

expect(getByTestId('details')).not.toHaveAttribute('open')
expect(await findByTestId('details')).not.toHaveAttribute('open')
})

it('Accurately passes down open state', () => {
it('Accurately passes down open state', async () => {
const Component = () => {
const {getDetailsProps, open} = useDetails({closeOnOutsideClick: true})
return (
Expand All @@ -46,12 +53,12 @@ describe('Details', () => {
)
}

const {getByTestId} = render(<Component />)
const {findByTestId} = render(<Component />)

document.body.click()

expect(getByTestId('summary')).toHaveTextContent('Closed')
expect(getByTestId('details')).not.toHaveAttribute('open')
expect(await findByTestId('summary')).toHaveTextContent('Closed')
expect(await findByTestId('details')).not.toHaveAttribute('open')
})

it('Can manipulate state with setOpen', async () => {
Expand Down Expand Up @@ -95,4 +102,53 @@ describe('Details', () => {

expect(getByTestId('summary')).toHaveTextContent('Open')
})

it('Adds default summary if no summary supplied', async () => {
const {getByText} = render(<Details data-testid="details">content</Details>)

expect(getByText('See Details')).toBeInTheDocument()
expect(getByText('See Details').tagName).toBe('SUMMARY')
})

it('Does not add default summary if summary supplied', async () => {
const {findByTestId, findByText} = render(
<Details data-testid="details">
<Details.Summary data-testid="summary">summary</Details.Summary>
content
</Details>,
)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(non blocker) Should this also be a dev warning? example of warning

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

superseeded by bac525a


await expect(findByText('See Details')).rejects.toThrow()
expect(await findByTestId('summary')).toBeInTheDocument()
expect((await findByTestId('summary')).tagName).toBe('SUMMARY')
})

it('Does not add default summary if supplied as different element', async () => {
const {findByTestId, findByText} = render(
<Details data-testid="details">
<Box as="summary" data-testid="summary">
custom summary
</Box>
content
</Details>,
)

await expect(findByText('See Details')).rejects.toThrow()
expect(await findByTestId('summary')).toBeInTheDocument()
expect((await findByTestId('summary')).tagName).toBe('SUMMARY')
})

describe('Details.Summary', () => {
behavesAsComponent({Component: Details.Summary, options: {skipSx: true}})

it('should support a custom `className` on the container element', () => {
render(<Details.Summary className="custom-class">test summary</Details.Summary>)
expect(screen.getByText('test summary')).toHaveClass('custom-class')
})

it('should pass extra props onto the container element', () => {
render(<Details.Summary data-testid="test">test summary</Details.Summary>)
expect(screen.getByText('test summary')).toHaveAttribute('data-testid', 'test')
})
})
})
Loading