diff --git a/astro.config.mjs b/astro.config.mjs index 86d09a0..d3ce0c5 100755 --- a/astro.config.mjs +++ b/astro.config.mjs @@ -32,6 +32,8 @@ export default defineConfig({ "@/shortcodes/Notice", "@/shortcodes/Video", "@/shortcodes/Youtube", + "@/shortcodes/Tabs", + "@/shortcodes/Tab", ], }), mdx(), diff --git a/src/content/pages/elements.mdx b/src/content/pages/elements.mdx index 1e14f3e..d942d7f 100755 --- a/src/content/pages/elements.mdx +++ b/src/content/pages/elements.mdx @@ -5,6 +5,24 @@ description: "this is meta description" draft: false --- +### Tab + + + + +1 + + + +2 + + + +3 + + + + # Heading 1 ## Heading 2 diff --git a/src/layouts/shortcodes/Accordion.tsx b/src/layouts/shortcodes/Accordion.tsx index 1eddea9..48ae362 100644 --- a/src/layouts/shortcodes/Accordion.tsx +++ b/src/layouts/shortcodes/Accordion.tsx @@ -1,4 +1,4 @@ -import { useState } from "react"; +import React, { useState } from "react"; const Accordion = ({ title, @@ -6,8 +6,8 @@ const Accordion = ({ className, }: { title: string; - children: any; - className: string | null; + children: React.ReactNode; + className?: string; }) => { const [show, setShow] = useState(false); diff --git a/src/layouts/shortcodes/Notice.tsx b/src/layouts/shortcodes/Notice.tsx index 6a0f22e..48c4364 100644 --- a/src/layouts/shortcodes/Notice.tsx +++ b/src/layouts/shortcodes/Notice.tsx @@ -1,6 +1,13 @@ import { humanize } from "@/lib/utils/textConverter"; +import React from "react"; -function Notice({ type, children }: { type: string; children: any }) { +function Notice({ + type, + children, +}: { + type: string; + children: React.ReactNode; +}) { return (
@@ -62,9 +69,9 @@ function Notice({ type, children }: { type: string; children: any }) { )} diff --git a/src/layouts/shortcodes/Tab.tsx b/src/layouts/shortcodes/Tab.tsx new file mode 100644 index 0000000..051f6ce --- /dev/null +++ b/src/layouts/shortcodes/Tab.tsx @@ -0,0 +1,7 @@ +import React from "react"; + +function Tab({ name, children }: { name: string; children: React.ReactNode }) { + return
{children}
; +} + +export default Tab; diff --git a/src/layouts/shortcodes/Tabs.tsx b/src/layouts/shortcodes/Tabs.tsx new file mode 100644 index 0000000..96b90c6 --- /dev/null +++ b/src/layouts/shortcodes/Tabs.tsx @@ -0,0 +1,59 @@ +import React, { useEffect, useRef } from "react"; + +function Tabs({ children }: { children: any }) { + //select tabItems + const tabItemsRef = useRef(null); + + const tabLinks = Array.from( + children.props.value.matchAll( + /]*>(.*?)<\/div>/gs + ), + (match: RegExpMatchArray) => ({ name: match[1], children: match[0] }) + ); + + console.log(tabLinks); + + //change tab item on click + const handleChangTab = (event: any, index: number) => { + const tabLinks = [...event.currentTarget.parentElement.children]; + const items = [...tabItemsRef.current!.children]; + const activeItem = items.find((item) => !item.classList.contains("hidden")); + const activeTabLink = tabLinks.find((item) => + item.classList.contains("active") + ); + if (activeItem === items[index]) return; + activeTabLink.classList.remove("active"); + event.currentTarget.classList.add("active"); + if (activeItem) { + activeItem.classList.add("hidden"); + } + items[index].classList.remove("hidden"); + }; + + //show first tab-item + useEffect(() => { + let allItems = [...tabItemsRef.current!.children]; + allItems[0].classList.remove("hidden"); + }, []); + + return ( +
+ {/*
    + {tabLinks.map((item: any, index: number) => ( +
  • handleChangTab(e, index)} + > + {item} +
  • + ))} +
+
+ {children} +
*/} +
+ ); +} + +export default Tabs; diff --git a/src/layouts/shortcodes/Video.tsx b/src/layouts/shortcodes/Video.tsx index d274e7d..3c22504 100644 --- a/src/layouts/shortcodes/Video.tsx +++ b/src/layouts/shortcodes/Video.tsx @@ -1,3 +1,4 @@ +import React from "react"; function Video({ title, width = 500, @@ -6,10 +7,10 @@ function Video({ ...rest }: { title: string; - width?: number; - height?: string; + width: number; + height: number | "auto"; src: string; - rest: any; + [key: string]: any; }) { return (