-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
feat: Update Expandable to use native <details> element
#13168
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
Merged
Merged
Changes from 1 commit
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -19,7 +19,7 @@ | |
| font-size: 0.85rem; | ||
| border: 0; | ||
| border-radius: 4px; | ||
| margin: 0 0 1rem; | ||
| margin: 0; | ||
| } | ||
|
|
||
| /** | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,115 @@ | ||
| 'use client'; | ||
|
|
||
| import {ReactNode, useEffect, useState} from 'react'; | ||
| import {ChevronDownIcon, ChevronRightIcon} from '@radix-ui/react-icons'; | ||
|
|
||
| // explicitly not usig CSS modules here | ||
| // because there's some prerendered content that depends on these exact class names | ||
| import '../callout/styles.scss'; | ||
| import styles from './style.module.scss'; | ||
|
|
||
| type Props = { | ||
| children: ReactNode; | ||
| title: string; | ||
| /** If defined, the expandable will be grouped with other expandables that have the same group. */ | ||
| group?: string; | ||
| level?: 'info' | 'warning' | 'success'; | ||
| permalink?: boolean; | ||
| }; | ||
|
|
||
| function slugify(str: string) { | ||
| return str | ||
| .toLowerCase() | ||
| .replace(/ /g, '-') | ||
| .replace(/[^a-z0-9-]/g, ''); | ||
| } | ||
|
|
||
| export function Expandable({title, level = 'info', children, permalink, group}: Props) { | ||
| const id = permalink ? slugify(title) : undefined; | ||
|
|
||
| const [isExpanded, setIsExpanded] = useState(false); | ||
|
|
||
| // Ensure we scroll to the element if the URL hash matches | ||
| useEffect(() => { | ||
| if (!id) { | ||
| return () => {}; | ||
| } | ||
|
|
||
| if (window.location.hash === `#${id}`) { | ||
| document.querySelector(`#${id}`)?.scrollIntoView(); | ||
| setIsExpanded(true); | ||
| } | ||
|
|
||
| // When the hash changes (e.g. when the back/forward browser buttons are used), | ||
| // we want to ensure to jump to the correct section | ||
| const onHashChange = () => { | ||
| if (window.location.hash === `#${id}`) { | ||
| setIsExpanded(true); | ||
| document.querySelector(`#${id}`)?.scrollIntoView(); | ||
| } | ||
| }; | ||
| // listen for hash changes and expand the section if the hash matches the title | ||
| window.addEventListener('hashchange', onHashChange); | ||
| return () => { | ||
| window.removeEventListener('hashchange', onHashChange); | ||
| }; | ||
| }, [id]); | ||
|
|
||
| function toggleIsExpanded(event: React.MouseEvent<HTMLDetailsElement>) { | ||
| const newVal = event.currentTarget.open; | ||
|
|
||
| if (id) { | ||
| if (newVal) { | ||
| window.history.pushState({}, '', `#${id}`); | ||
| } else { | ||
| window.history.pushState({}, '', '#'); | ||
| } | ||
| } | ||
|
|
||
| setIsExpanded(newVal); | ||
| } | ||
|
|
||
| return ( | ||
| <details | ||
| name={group} | ||
| className={`${styles.expandable} callout !block ${'callout-' + level}`} | ||
| open={isExpanded} | ||
| // We only need this to keep the URL hash in sync | ||
| onToggle={id ? toggleIsExpanded : undefined} | ||
| id={id} | ||
| > | ||
| <summary className={`${styles['expandable-header']} callout-header`}> | ||
| <ChevronDownIcon | ||
| className={`${styles['expandable-icon-expanded']} callout-icon`} | ||
| /> | ||
| <ChevronRightIcon | ||
| className={`${styles['expandable-icon-collapsed']} callout-icon`} | ||
| /> | ||
| <div>{title}</div> | ||
| </summary> | ||
| <div className={`${styles['expandable-body']} callout-body content-flush-bottom`}> | ||
| {children} | ||
| </div> | ||
| </details> | ||
| ); | ||
| } | ||
|
|
||
| // | ||
| // | ||
| // <details name={group} className={`callout !block ${'callout-' + level}`}> | ||
| // <summary className="callout-header">{title}</summary> | ||
| // <div className="callout-body content-flush-bottom">{children}</div> | ||
| // </details> | ||
| // | ||
| // | ||
|
|
||
| // | ||
| // <Callout | ||
| // level={level} | ||
| // title={title} | ||
| // Icon={isExpanded ? ChevronDownIcon : ChevronRightIcon} | ||
| // id={id} | ||
| // titleOnClick={toggleIsExpanded} | ||
| // > | ||
| // {isExpanded ? children : undefined} | ||
| // </Callout> | ||
mydea marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| .expandable-header { | ||
| display: flex; | ||
| cursor: pointer; | ||
| gap: 0.7rem; | ||
| margin-bottom: 0.3rem; | ||
| } | ||
|
|
||
| .expandable-body { | ||
| margin-left: 1.6rem; | ||
|
|
||
| & ul, & ol { | ||
| padding-left: 1.2rem; | ||
| } | ||
| } | ||
|
|
||
| .expandable { | ||
| &:open { | ||
| padding-bottom: 1rem; | ||
| } | ||
|
|
||
| &:not(:open) .expandable-header { | ||
| margin-bottom: 0; | ||
| } | ||
|
|
||
| &:open .expandable-icon-collapsed { | ||
| display: none; | ||
| } | ||
|
|
||
| &:not(:open) .expandable-icon-expanded { | ||
| display: none; | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just curious, why are we getting rid of the bottom-margin here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sorry, should have explained: This was a bit hard to work with, as this element is nested in a bunch of other divs, making it impossible to remove this, basically. Also, as far as I've seen this is always in another div that also has margin, so this was redundant - I scanned a bunch of pages and it did not seem to affect the styling, so hopefully this is good 🤞
Without this change, having a code block in an expandable (see example in the component page) looked weird because there was always a margin on bottom.