-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add collapsible section for secondary information (#839)
- Loading branch information
1 parent
8f3d09c
commit c4faef7
Showing
4 changed files
with
141 additions
and
68 deletions.
There are no files selected for viewing
This file contains 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 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,21 @@ | ||
section.collapsible > header { | ||
display: flex; | ||
justify-content: space-between; | ||
align-items: flex-start; | ||
} | ||
|
||
section.collapsible > header > * { | ||
margin: 0; | ||
} | ||
|
||
section.collapsible > header { | ||
margin-bottom: 0.5rem; | ||
} | ||
|
||
section.collapsible.collapsed > header { | ||
margin-bottom: 0; | ||
} | ||
|
||
section.collapsible > header > button { | ||
padding: 0; | ||
} |
This file contains 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,47 @@ | ||
import cx from 'classnames' | ||
import { HelpCircle, X } from 'lucide-preact' | ||
import { nanoid } from 'nanoid' | ||
import type { ComponentChildren } from 'preact' | ||
import { useRef, useState } from 'preact/hooks' | ||
|
||
import './Collapsible.css' | ||
|
||
export const Collapsible = ({ | ||
children, | ||
title, | ||
collapsed, | ||
icon, | ||
class: className, | ||
moreText, | ||
}: { | ||
children: ComponentChildren | ||
title: ComponentChildren | ||
collapsed?: boolean | ||
icon?: ComponentChildren | ||
class?: string | ||
moreText?: string | ||
}) => { | ||
const [isCollapsed, setIsCollapsed] = useState<boolean>(collapsed ?? true) | ||
const id = useRef(nanoid()) | ||
|
||
return ( | ||
<section | ||
class={cx('collapsible', className, { collapsed: isCollapsed })} | ||
id={id.current} | ||
> | ||
<header> | ||
{title} | ||
<button | ||
type="button" | ||
class="btn btn-link" | ||
onClick={() => setIsCollapsed(!isCollapsed)} | ||
title={isCollapsed ? (moreText ?? 'show more') : 'close'} | ||
aria-controls={id.current} | ||
> | ||
{isCollapsed ? icon === undefined ? <HelpCircle /> : icon : <X />} | ||
</button> | ||
</header> | ||
{!isCollapsed && <main>{children}</main>} | ||
</section> | ||
) | ||
} |
This file contains 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