Skip to content

Commit

Permalink
docs: fix copy in code blocks with tabs (medusajs#10123)
Browse files Browse the repository at this point in the history
* docs: fix copy in code blocks with tabs

* fix lint error
  • Loading branch information
shahednasser authored Nov 15, 2024
1 parent 1fb2998 commit b0c6efa
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 9 deletions.
50 changes: 41 additions & 9 deletions www/packages/docs-ui/src/components/CodeTabs/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,36 @@ export const CodeTabs = ({
return "source" in typedProps
}

const getCodeBlockProps = (
codeBlock: React.ReactElement<
unknown,
string | React.JSXElementConstructor<any>
>
): CodeBlockProps | undefined => {
if (typeof codeBlock.props !== "object" || !codeBlock.props) {
return undefined
}

if ("source" in codeBlock.props) {
return codeBlock.props as CodeBlockProps
}

if (
"children" in codeBlock.props &&
typeof codeBlock.props.children === "object" &&
codeBlock.props.children
) {
return getCodeBlockProps(
codeBlock.props.children as React.ReactElement<
unknown,
string | React.JSXElementConstructor<any>
>
)
}

return undefined
}

const tabs: CodeTab[] = useMemo(() => {
const tempTabs: CodeTab[] = []
Children.forEach(children, (child) => {
Expand All @@ -79,10 +109,19 @@ export const CodeTabs = ({

const codeBlockProps = codeBlock.props as CodeBlockProps

const modifiedProps: CodeBlockProps = {
...(getCodeBlockProps(codeBlock) || {
source: "",
}),
badgeLabel: undefined,
hasTabs: true,
className: clsx("!my-0", codeBlockProps.className),
}

tempTabs.push({
label: typedChildProps.label,
value: typedChildProps.value,
codeProps: codeBlockProps,
codeProps: modifiedProps,
codeBlock: {
...codeBlock,
props: {
Expand All @@ -91,14 +130,7 @@ export const CodeTabs = ({
...(typeof codeBlockProps.children === "object"
? codeBlockProps.children
: {}),
props: {
...(React.isValidElement(codeBlockProps.children)
? (codeBlockProps.children.props as Record<string, unknown>)
: {}),
badgeLabel: undefined,
hasTabs: true,
className: clsx("!my-0", codeBlockProps.className),
},
props: modifiedProps,
},
},
},
Expand Down
1 change: 1 addition & 0 deletions www/packages/docs-ui/src/components/Npm2YarnCode/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export const Npm2YarnCode = ({ npmCode, ...rest }: Npm2YarnCodeProps) => {
const lang = "bash"

const { title = "", ...codeOptions } = rest
codeOptions.hasTabs = true

const tabs = [
{
Expand Down

0 comments on commit b0c6efa

Please sign in to comment.