Skip to content

Commit 51894fe

Browse files
authored
feat: Update Expandable to use native <details> element (#13168)
* feat: Update `Expandable` to use native `<details>` element This has some important benefits: 1. It should be covered by search etc. as this is statically rendered now and just "hidden" via CSS. 2. The browser automatically searches in it when using CMD+F - it will auto-open respective details that contain the searched content. * fix link * remove dead code, oops * oops fix it
1 parent f657655 commit 51894fe

File tree

7 files changed

+72
-15
lines changed

7 files changed

+72
-15
lines changed

docs/contributing/pages/components.mdx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,12 @@ Render an expandable section to provide additional information to users on deman
130130
provide optional information that can help users be more successful.
131131
</Expandable>
132132

133+
<Expandable title="Expandable with a code block">
134+
```js
135+
const foo = 'bar';
136+
```
137+
</Expandable>
138+
133139
```markdown {tabTitle:Example}
134140
<Expandable title="Here's something worth noting">
135141
This is an expandable section in an `'info'` alert style.

docs/platforms/javascript/common/troubleshooting/index.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ To fix this, change the `tracePropagationTargets` option during SDK initializati
105105
</PlatformCategorySection>
106106

107107
<PlatformCategorySection notSupported={['browser']}>
108-
<PlatformSection notSupported={['javascript.aws-lambda', 'javascript.azure-functions', 'javascript.bun', 'javascript.cloudflare', 'javascript.deno', 'javascript.electron', 'javascript.gcp-functions']}>
108+
<PlatformSection notSupported={['javascript.aws-lambda', 'javascript.azure-functions', 'javascript.bun', 'javascript.cloudflare', 'javascript.deno', 'javascript.electron', 'javascript.gcp-functions', 'javascript.tanstackstart-react']}>
109109
<Expandable permalink title="Error: 'import-in-the-middle' failed to wrap">
110110
When using ESM, by default all packages are wrapped under the hood by
111111
[import-in-the-middle](https://www.npmjs.com/package/import-in-the-middle).

src/components/codeBlock/code-blocks.module.scss

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
font-size: 0.85rem;
2020
border: 0;
2121
border-radius: 4px;
22-
margin: 0 0 1rem;
22+
margin: 0;
2323
}
2424

2525
/**

src/components/codeTabs.tsx

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,8 +134,6 @@ export function CodeTabs({children}: CodeTabProps) {
134134
}
135135

136136
const Container = styled('div')`
137-
margin-bottom: 1.5rem;
138-
139137
pre[class*='language-'] {
140138
padding: 10px 12px;
141139
border-radius: 0 0 3px 3px;

src/components/docPage/type.scss

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,10 @@
136136
font-weight: 500;
137137
}
138138

139+
.code-tabs-wrapper:not(:last-child) {
140+
margin-bottom: 1.5rem;
141+
}
142+
139143
.anchorlink,
140144
.anchorlink.before {
141145
display: flex;

src/components/expandable.tsx renamed to src/components/expandable/index.tsx

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,16 @@
33
import {ReactNode, useEffect, useState} from 'react';
44
import {ChevronDownIcon, ChevronRightIcon} from '@radix-ui/react-icons';
55

6-
import {Callout} from './callout';
6+
// explicitly not usig CSS modules here
7+
// because there's some prerendered content that depends on these exact class names
8+
import '../callout/styles.scss';
9+
import styles from './style.module.scss';
710

811
type Props = {
912
children: ReactNode;
1013
title: string;
14+
/** If defined, the expandable will be grouped with other expandables that have the same group. */
15+
group?: string;
1116
level?: 'info' | 'warning' | 'success';
1217
permalink?: boolean;
1318
};
@@ -19,7 +24,7 @@ function slugify(str: string) {
1924
.replace(/[^a-z0-9-]/g, '');
2025
}
2126

22-
export function Expandable({title, level, children, permalink}: Props) {
27+
export function Expandable({title, level = 'info', children, permalink, group}: Props) {
2328
const id = permalink ? slugify(title) : undefined;
2429

2530
const [isExpanded, setIsExpanded] = useState(false);
@@ -50,8 +55,8 @@ export function Expandable({title, level, children, permalink}: Props) {
5055
};
5156
}, [id]);
5257

53-
function toggleIsExpanded() {
54-
const newVal = !isExpanded;
58+
function toggleIsExpanded(event: React.MouseEvent<HTMLDetailsElement>) {
59+
const newVal = event.currentTarget.open;
5560

5661
if (id) {
5762
if (newVal) {
@@ -65,14 +70,26 @@ export function Expandable({title, level, children, permalink}: Props) {
6570
}
6671

6772
return (
68-
<Callout
69-
level={level}
70-
title={title}
71-
Icon={isExpanded ? ChevronDownIcon : ChevronRightIcon}
73+
<details
74+
name={group}
75+
className={`${styles.expandable} callout !block ${'callout-' + level}`}
76+
open={isExpanded}
77+
// We only need this to keep the URL hash in sync
78+
onToggle={id ? toggleIsExpanded : undefined}
7279
id={id}
73-
titleOnClick={toggleIsExpanded}
7480
>
75-
{isExpanded ? children : undefined}
76-
</Callout>
81+
<summary className={`${styles['expandable-header']} callout-header`}>
82+
<ChevronDownIcon
83+
className={`${styles['expandable-icon-expanded']} callout-icon`}
84+
/>
85+
<ChevronRightIcon
86+
className={`${styles['expandable-icon-collapsed']} callout-icon`}
87+
/>
88+
<div>{title}</div>
89+
</summary>
90+
<div className={`${styles['expandable-body']} callout-body content-flush-bottom`}>
91+
{children}
92+
</div>
93+
</details>
7794
);
7895
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
.expandable-header {
2+
display: flex;
3+
cursor: pointer;
4+
gap: 0.7rem;
5+
margin-bottom: 0.3rem;
6+
}
7+
8+
.expandable-body {
9+
margin-left: 1.6rem;
10+
11+
& ul, & ol {
12+
padding-left: 1.2rem;
13+
}
14+
}
15+
16+
.expandable {
17+
&:open {
18+
padding-bottom: 1rem;
19+
}
20+
21+
&:not(:open) .expandable-header {
22+
margin-bottom: 0;
23+
}
24+
25+
&:open .expandable-icon-collapsed {
26+
display: none;
27+
}
28+
29+
&:not(:open) .expandable-icon-expanded {
30+
display: none;
31+
}
32+
}

0 commit comments

Comments
 (0)