Skip to content

Commit d682b56

Browse files
authored
refactor: remove useless code (#2916)
* refactor: remove useless code * enhanced
1 parent 4f25153 commit d682b56

File tree

2 files changed

+70
-49
lines changed

2 files changed

+70
-49
lines changed

src/components/Steps/step-content.tsx

Lines changed: 35 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,20 @@
11
// Copyright 2023 DatabendLabs.
2-
import React, { FC, ReactElement, ReactNode, useEffect, useRef, useState } from "react";
2+
import React, {
3+
FC,
4+
ReactElement,
5+
ReactNode,
6+
useEffect,
7+
useMemo,
8+
useRef,
9+
useState,
10+
} from "react";
311
import LinkSvg from "../../../static/icons/link";
412
import copy from "copy-to-clipboard";
513
import Tooltip from "../BaseComponents/Tooltip";
6-
// import { MDXProvider } from '@mdx-js/react';
14+
import DownArrow from "@site/static/icons/down.svg";
715
interface IProps {
816
number: number | string;
9-
children: ReactNode;
17+
children: ReactNode | ReactNode[];
1018
title: string;
1119
outLink?: string;
1220
defaultCollapsed?: boolean;
@@ -16,10 +24,9 @@ const StepContent: FC<IProps> = ({
1624
children,
1725
title,
1826
outLink,
19-
defaultCollapsed = false,
27+
defaultCollapsed = undefined,
2028
}): ReactElement => {
2129
const wrapRef = useRef<any>(null);
22-
const contentRef = useRef<any>(null);
2330
const [isCollapsed, setIsCollapsed] = useState(defaultCollapsed);
2431

2532
useEffect(() => {
@@ -44,48 +51,14 @@ const StepContent: FC<IProps> = ({
4451
);
4552
}
4653
}
47-
48-
// Add collapse button after first h2 if defaultCollapsed is defined
49-
if (defaultCollapsed !== undefined && contentRef?.current) {
50-
const firstH2 = contentRef.current.querySelector('h2');
51-
if (firstH2 && !firstH2.nextElementSibling?.classList?.contains('collapse-button-wrapper')) {
52-
const buttonWrapper = document.createElement('div');
53-
buttonWrapper.className = 'collapse-button-wrapper';
54-
buttonWrapper.style.cssText = 'display: flex; justify-content: flex-start; margin: 8px 0 16px 0;';
55-
56-
const button = document.createElement('button');
57-
button.style.cssText = 'padding: 4px 12px; cursor: pointer; background: transparent; border: 1px solid var(--ifm-color-emphasis-300); border-radius: 4px; font-size: 12px; color: var(--ifm-color-emphasis-700);';
58-
button.textContent = isCollapsed ? '▼ Show Details' : '▲ Hide Details';
59-
button.onclick = () => setIsCollapsed(prev => !prev);
60-
61-
buttonWrapper.appendChild(button);
62-
firstH2.parentNode.insertBefore(buttonWrapper, firstH2.nextSibling);
63-
}
54+
}, []);
55+
const childrenArray = useMemo(() => {
56+
if (Array.isArray(children)) {
57+
return children;
58+
} else {
59+
return [children];
6460
}
65-
}, [defaultCollapsed]);
66-
useEffect(() => {
67-
// Update button text and hide/show content after button when collapsed
68-
if (contentRef?.current) {
69-
const buttonWrapper = contentRef.current.querySelector('.collapse-button-wrapper');
70-
if (buttonWrapper) {
71-
const button = buttonWrapper.querySelector('button');
72-
if (button) {
73-
button.textContent = isCollapsed ? '▼ Show Details' : '▲ Hide Details';
74-
// Re-bind the click event with function form to avoid closure issues
75-
button.onclick = () => setIsCollapsed(prev => !prev);
76-
}
77-
78-
let node = buttonWrapper.nextSibling;
79-
while (node) {
80-
if (node.nodeType === 1) { // Element node
81-
(node as HTMLElement).style.display = isCollapsed ? 'none' : '';
82-
}
83-
node = node.nextSibling;
84-
}
85-
}
86-
}
87-
}, [isCollapsed]);
88-
61+
}, [children]);
8962
return (
9063
<div
9164
className="global-step-container"
@@ -132,8 +105,22 @@ const StepContent: FC<IProps> = ({
132105
)}
133106
</span>
134107
<div className="step-content" ref={wrapRef}>
135-
<div ref={contentRef}>
136-
{children}
108+
<div>
109+
{isCollapsed !== undefined && (
110+
<button
111+
onClick={() => setIsCollapsed((v) => !v)}
112+
className="collapsible-btn"
113+
aria-expanded={!isCollapsed}
114+
>
115+
<span className={`collapsible-icon ${isCollapsed ? "" : "open"}`}>
116+
<DownArrow width={16} height={16} />
117+
</span>
118+
{isCollapsed ? "Show Details" : "Hide Details"}
119+
</button>
120+
)}
121+
{childrenArray?.[0]}
122+
{(isCollapsed === undefined || !isCollapsed) &&
123+
childrenArray?.slice(1)}
137124
</div>
138125
</div>
139126
</div>

src/css/steps.scss

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,8 @@
7474
background-color: hsl(var(--muted));
7575
text-align: center;
7676
text-indent: -1px;
77-
font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, Liberation Mono, Courier New, monospace;
77+
font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas,
78+
Liberation Mono, Courier New, monospace;
7879
font-size: 1rem;
7980
line-height: 1.5rem;
8081
font-weight: 500;
@@ -93,6 +94,39 @@
9394
display: none;
9495
}
9596
.step-content {
97+
.collapsible-btn {
98+
position: relative;
99+
top: -15px;
100+
display: inline-flex;
101+
align-items: center;
102+
gap: 6px;
103+
padding: 2px 8px;
104+
font-size: 0.875rem;
105+
font-weight: 500;
106+
color: #374151;
107+
background: var(--color-fill-0);
108+
border: 1px solid var(--color-border);
109+
border-radius: 6px;
110+
cursor: pointer;
111+
transition: all 0.2s ease;
112+
}
113+
.collapsible-btn:focus-visible {
114+
outline: none;
115+
box-shadow: 0 0 0 3px rgba(59, 130, 246, 0.3);
116+
}
117+
118+
.collapsible-icon {
119+
display: flex;
120+
transition: transform 0.2s ease;
121+
}
122+
123+
.collapsible-icon.open {
124+
transform: rotate(180deg);
125+
}
126+
.collapsible-content {
127+
overflow: hidden;
128+
transition: max-height 0.3s ease-in-out;
129+
}
96130
h3:first-child {
97131
color: var(--h3-title-color);
98132
font-size: 1.25rem;

0 commit comments

Comments
 (0)