metacubexd/src/components/Collapse.tsx

51 lines
1.3 KiB
TypeScript
Raw Normal View History

2023-09-16 01:28:58 +08:00
import { children, JSX, ParentComponent, Show } from 'solid-js'
2023-08-30 23:02:55 +08:00
import { twMerge } from 'tailwind-merge'
type Props = {
title: JSX.Element
2023-09-07 10:41:42 +08:00
isOpen?: boolean
2023-08-30 23:02:55 +08:00
onCollapse: (collapsed: boolean) => void
}
export const Collapse: ParentComponent<Props> = (props) => {
2023-09-16 01:28:58 +08:00
const { title, onCollapse } = props
2023-08-30 23:02:55 +08:00
const getCollapseClassName = () => {
2023-08-30 23:36:25 +08:00
const openedClassName = 'collapse-open'
const closedClassName = 'collapse-close'
2023-08-30 23:36:25 +08:00
return props.isOpen ? openedClassName : closedClassName
2023-08-30 23:02:55 +08:00
}
2023-08-30 23:36:25 +08:00
2023-08-30 23:02:55 +08:00
const getCollapseContentClassName = () => {
2023-08-30 23:36:25 +08:00
const openedClassName = 'opacity-100'
const closedClassName = 'opacity-0'
2023-08-30 23:36:25 +08:00
return props.isOpen ? openedClassName : closedClassName
2023-08-30 23:02:55 +08:00
}
return (
<div
class={twMerge(
getCollapseClassName(),
'collapse collapse-arrow select-none overflow-visible border-secondary bg-base-200 drop-shadow-md',
2023-08-30 23:02:55 +08:00
)}
>
<div
class="collapse-title pr-4 text-xl font-medium after:!top-8"
onClick={() => onCollapse(!props.isOpen)}
>
{title}
</div>
2023-08-30 23:02:55 +08:00
<div
class={twMerge(
getCollapseContentClassName(),
'collapse-content grid grid-cols-2 gap-2 transition-opacity duration-1000 lg:grid-cols-3 xl:grid-cols-4',
2023-08-30 23:02:55 +08:00
)}
>
2023-09-16 01:28:58 +08:00
<Show when={props.isOpen}>{children(() => props.children)()}</Show>
2023-08-30 23:02:55 +08:00
</div>
</div>
)
}