Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
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
6 changes: 6 additions & 0 deletions docs/contributing/pages/components.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,12 @@ Render an expandable section to provide additional information to users on deman
provide optional information that can help users be more successful.
</Expandable>

<Expandable title="Expandable with a code block">
```js
const foo = 'bar';
```
</Expandable>

```markdown {tabTitle:Example}
<Expandable title="Here's something worth noting">
This is an expandable section in an `'info'` alert style.
Expand Down
2 changes: 1 addition & 1 deletion src/components/codeBlock/code-blocks.module.scss
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
font-size: 0.85rem;
border: 0;
border-radius: 4px;
margin: 0 0 1rem;
margin: 0;
Copy link
Contributor

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?

Copy link
Member Author

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.

}

/**
Expand Down
2 changes: 0 additions & 2 deletions src/components/codeTabs.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -134,8 +134,6 @@ export function CodeTabs({children}: CodeTabProps) {
}

const Container = styled('div')`
margin-bottom: 1.5rem;

pre[class*='language-'] {
padding: 10px 12px;
border-radius: 0 0 3px 3px;
Expand Down
4 changes: 4 additions & 0 deletions src/components/docPage/type.scss
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,10 @@
font-weight: 500;
}

.code-tabs-wrapper:not(:last-child) {
margin-bottom: 1.5rem;
}

.anchorlink,
.anchorlink.before {
display: flex;
Expand Down
78 changes: 0 additions & 78 deletions src/components/expandable.tsx

This file was deleted.

115 changes: 115 additions & 0 deletions src/components/expandable/index.tsx
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>
32 changes: 32 additions & 0 deletions src/components/expandable/style.module.scss
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;
}
}
Loading