From 4116060f4c5daa4f84218ceadf0031fb759cc9c7 Mon Sep 17 00:00:00 2001 From: Innei Date: Fri, 8 Sep 2023 13:53:00 +0800 Subject: [PATCH] feat: add collapse background --- src/components/ui/button/StyledButton.tsx | 6 +- src/components/ui/collapse/Collapse.tsx | 53 ++++++++++------ src/components/ui/collapse/index.demo.tsx | 60 +++++++++++++++++++ .../ui/markdown/renderers/collapse.tsx | 2 +- 4 files changed, 100 insertions(+), 21 deletions(-) create mode 100644 src/components/ui/collapse/index.demo.tsx diff --git a/src/components/ui/button/StyledButton.tsx b/src/components/ui/button/StyledButton.tsx index 1c3df0f3af..96d6da41f7 100644 --- a/src/components/ui/button/StyledButton.tsx +++ b/src/components/ui/button/StyledButton.tsx @@ -2,6 +2,7 @@ import clsx from 'clsx' import Link from 'next/link' import { tv } from 'tailwind-variants' +import type { FC } from 'react' import { MotionButtonBase } from './MotionButton' @@ -32,12 +33,13 @@ type SharedProps = { className?: string } type ButtonProps = SharedProps & (NativeButtonProps | NativeLinkProps) -export function StyledButton({ + +export const StyledButton: FC = ({ variant = 'primary', className, href, ...props -}: ButtonProps) { +}) => { return href ? ( = ({ isOpened, className, children, -}: React.PropsWithChildren<{ isOpened: boolean } & { className?: string }>) => { - // By using `AnimatePresence` to mount and unmount the contents, we can animate - // them in and out while also only rendering the contents of open accordions + + withBackground = false, +}) => { + const variants = React.useMemo(() => { + const v = { + open: { + opacity: 1, + height: 'auto', + transition: microReboundPreset, + }, + collapsed: { + opacity: 0, + height: 0, + overflow: 'hidden', + }, + } satisfies Variants + + if (withBackground) { + // @ts-expect-error + v.open.background = `hsl(var(--a) / 10%)` + // @ts-expect-error + v.collapsed.background = `hsl(var(--a) / 0%)` + } + + return v + }, [withBackground]) return ( <> @@ -21,21 +49,10 @@ export const Collapse = ({ initial="collapsed" animate="open" exit="collapsed" - variants={{ - open: { - opacity: 1, - height: 'auto', - transition: microReboundPreset, - }, - collapsed: { - opacity: 0, - height: 0, - overflow: 'hidden', - }, - }} - className={className} + variants={variants} + className={clsxm(withBackground && 'rounded-lg', className)} > - {children} + {withBackground ?
{children}
: children} )}
diff --git a/src/components/ui/collapse/index.demo.tsx b/src/components/ui/collapse/index.demo.tsx new file mode 100644 index 0000000000..3f599a12b4 --- /dev/null +++ b/src/components/ui/collapse/index.demo.tsx @@ -0,0 +1,60 @@ +import React from 'react' +import { domMax, LazyMotion } from 'framer-motion' +import type { DocumentComponent } from 'storybook/typings' + +import { StyledButton } from '../button' +import { Collapse } from './Collapse' + +export const CollapseDemo1: DocumentComponent = () => { + const [opened, setOpened] = React.useState(false) + return ( + + setOpened((opened) => !opened)}> + Toggle Collapse + + +

+ Maiores occaecati quis animi nihil debitis. Iure suscipit animi. + Repellat quia quas harum possimus dolorum dolore ullam eius. Tenetur + aut saepe illo expedita culpa. Nisi asperiores doloribus facere + eveniet ad tempore nemo accusantium in. Possimus eum dolorum a aliquid + unde dolore corporis. Voluptatem quibusdam ipsam numquam. Vero aliquid + odit reiciendis amet cum sapiente commodi. Natus in ullam dignissimos + sed eos accusantium. Quis eligendi aliquid. Cumque possimus sed + suscipit vero. Repellendus inventore quo porro necessitatibus totam. +

+
+
+ ) +} + +CollapseDemo1.meta = { + title: 'Normal Collapse', +} + +export const CollapseDemo2: DocumentComponent = () => { + const [opened, setOpened] = React.useState(false) + return ( + + setOpened((opened) => !opened)}> + Toggle Collapse + + +

+ Maiores occaecati quis animi nihil debitis. Iure suscipit animi. + Repellat quia quas harum possimus dolorum dolore ullam eius. Tenetur + aut saepe illo expedita culpa. Nisi asperiores doloribus facere + eveniet ad tempore nemo accusantium in. Possimus eum dolorum a aliquid + unde dolore corporis. Voluptatem quibusdam ipsam numquam. Vero aliquid + odit reiciendis amet cum sapiente commodi. Natus in ullam dignissimos + sed eos accusantium. Quis eligendi aliquid. Cumque possimus sed + suscipit vero. Repellendus inventore quo porro necessitatibus totam. +

+
+
+ ) +} + +CollapseDemo2.meta = { + title: 'With Background Collapse', +} diff --git a/src/components/ui/markdown/renderers/collapse.tsx b/src/components/ui/markdown/renderers/collapse.tsx index 74a8bc64a1..5968088ce9 100644 --- a/src/components/ui/markdown/renderers/collapse.tsx +++ b/src/components/ui/markdown/renderers/collapse.tsx @@ -30,7 +30,7 @@ export const MDetails: FC<{ children: ReactNode[] }> = (props) => { {$head} - +