Skip to content

Commit f815c7e

Browse files
committed
Support using VerticalStepper with H3
1 parent 53575ee commit f815c7e

File tree

2 files changed

+25
-7
lines changed

2 files changed

+25
-7
lines changed

plugins/remark-custom-blocks.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ const extractText = (nodes) => {
1818
const plugin = (options) => {
1919
const transformer = (tree, file) => {
2020

21+
let total_steps = 0; // Keep track of the total number of steps
22+
2123
// Target JSX elements in the AST
2224
visit(tree, 'mdxJsxFlowElement', (node, index, parent) => {
2325
// Look specifically for the <VerticalStepper> tag used in the markdown file
@@ -27,12 +29,16 @@ const plugin = (options) => {
2729
const jsxAttributes = node.attributes || [];
2830
let type = "numbered"; // Default type
2931
let isExpanded = true;
32+
let headerLevel = 2;
3033

3134
// Extract attributes
3235
jsxAttributes.forEach(attr => {
3336
if (attr.type === 'mdxJsxAttribute') {
3437
if (attr.name === 'type' && typeof attr.value === 'string') {
3538
type = attr.value;
39+
} else if (attr.name === 'headerLevel' && typeof attr.value === 'string') {
40+
if (attr.value === "h3")
41+
headerLevel = 3
3642
}
3743
}
3844
});
@@ -52,18 +58,19 @@ const plugin = (options) => {
5258
anchorId: currentAnchorId,
5359
content: [...currentStepContent],
5460
});
61+
total_steps++;
5562
}
5663
currentStepContent = [];
5764
currentStepLabel = null; // Reset label
5865
};
5966

6067
if (node.children && node.children.length > 0) {
6168
node.children.forEach((child) => {
62-
if (child.type === 'heading' && child.depth === 2) {
69+
if (child.type === 'heading' && child.depth === headerLevel) {
6370
finalizeStep(); // Finalize the previous step first
6471
currentStepLabel = extractText(child.children);
6572
currentAnchorId = child.data.hProperties.id;
66-
currentStepId = `step-${stepsData.length + 1}`; // Generate step-X ID
73+
currentStepId = `step-${total_steps}`; // Generate step-X ID
6774
currentStepContent.push(child); // We need the header otherwise onBrokenAnchors fails
6875
} else if (currentStepLabel) {
6976
// Only collect content nodes *after* a heading has defined a step
@@ -81,6 +88,7 @@ const plugin = (options) => {
8188
// Set attributes
8289
node.attributes = [
8390
{ type: 'mdxJsxAttribute', name: 'type', value: type },
91+
{ type: 'mdxJsxAttribute', name: 'headerLevel', value: headerLevel },
8492
];
8593
if (isExpanded) {
8694
node.attributes.push({

src/components/Stepper/Stepper.tsx

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ interface StepProps {
88
label?: React.ReactNode;
99
forceExpanded?: string; // From parent 'expanded' state
1010
isFirstStep?: boolean; // Prop calculated by parent
11+
headerType?: string;
1112
[key: string]: any;
1213
}
1314

@@ -17,6 +18,7 @@ const Step = ({
1718
label,
1819
forceExpanded,
1920
isFirstStep = false,
21+
headerType,
2022
...restProps
2123
}: StepProps) => {
2224

@@ -34,14 +36,14 @@ const Step = ({
3436
const divChildren = Array.from(button.children).filter(el => el.tagName === 'DIV');
3537
const label = divChildren[1];
3638
const content = button.nextElementSibling;
37-
const header = content.querySelectorAll('h2')[0]
39+
const header = content.querySelectorAll(headerType)[0]
3840
header.style.margin = '0';
3941
button.append(header)
4042
label.remove()
4143
} catch (e) {
42-
console.log('Error occurred in Stepper.tsx while swapping H2 for Click-UI label')
44+
console.log(`Error occurred in Stepper.tsx while swapping ${headerType} for Click-UI label:`, e)
4345
}
44-
}, [id]);
46+
}, [id, headerType]);
4547

4648
// Filter out props specific to this wrapper logic
4749
const {
@@ -69,6 +71,7 @@ interface StepperProps {
6971
type?: 'numbered' | 'bulleted';
7072
className?: string;
7173
expanded?: string; // Corresponds to allExpanded in MDX
74+
headerLevel?: number;
7275
[key: string]: any;
7376
}
7477

@@ -77,13 +80,19 @@ const VStepper = ({
7780
children,
7881
type = 'numbered',
7982
className,
80-
expanded, // 'true' if allExpanded was set
83+
expanded,
84+
headerLevel,
8185
...props
8286
}: StepperProps) => {
8387

8488
// Determine if all steps should be expanded from the start
8589
const isExpandedMode = expanded === 'true';
8690

91+
let hType = 'h2';
92+
if (headerLevel == 3) {
93+
hType = 'h3'
94+
}
95+
8796
// Get children and filter out non-elements
8897
const childSteps = React.Children.toArray(children)
8998
.filter(child => React.isValidElement(child));
@@ -104,7 +113,8 @@ const VStepper = ({
104113
key: stepId,
105114
id: stepId,
106115
isFirstStep, // Pass down flag for first step logic
107-
forceExpanded: isExpandedMode ? 'true' : undefined // Pass down expanded mode
116+
forceExpanded: isExpandedMode ? 'true' : undefined, // Pass down expanded mode
117+
headerType: hType
108118
});
109119
});
110120

0 commit comments

Comments
 (0)