webapp/src/components/_shared/Popup/Popup.tsx

56 lines
1.6 KiB
TypeScript
Raw Normal View History

2022-11-10 12:50:47 +00:00
import { createEffect, createSignal, JSX, Show } from 'solid-js'
2022-10-25 15:36:32 +00:00
import styles from './Popup.module.scss'
import { clsx } from 'clsx'
2022-11-20 21:25:59 +00:00
import { useOutsideClickHandler } from '../../../utils/useOutsideClickHandler'
2022-12-17 03:27:00 +00:00
import { set } from 'husky'
2022-10-25 15:36:32 +00:00
type HorizontalAnchor = 'center' | 'right'
2022-10-25 15:36:32 +00:00
export type PopupProps = {
containerCssClass?: string
trigger: JSX.Element
children: JSX.Element
onVisibilityChange?: (isVisible) => void
horizontalAnchor?: HorizontalAnchor
2022-12-17 03:27:00 +00:00
variant?: 'bordered' | 'tiny'
2022-10-17 20:53:04 +00:00
}
export const Popup = (props: PopupProps) => {
2022-10-25 15:36:32 +00:00
const [isVisible, setIsVisible] = createSignal(false)
const horizontalAnchor: HorizontalAnchor = props.horizontalAnchor || 'center'
2022-10-17 20:53:04 +00:00
2022-10-25 15:36:32 +00:00
createEffect(() => {
if (props.onVisibilityChange) {
props.onVisibilityChange(isVisible())
}
2022-10-17 20:53:04 +00:00
})
const containerRef: { current: HTMLElement } = { current: null }
2022-10-25 15:36:32 +00:00
useOutsideClickHandler({
containerRef,
predicate: () => isVisible(),
handler: () => setIsVisible(false)
2022-10-17 20:53:04 +00:00
})
2022-10-25 15:36:32 +00:00
const toggle = () => setIsVisible((oldVisible) => !oldVisible)
2022-10-17 20:53:04 +00:00
return (
<span class={clsx(styles.container, props.containerCssClass)} ref={(el) => (containerRef.current = el)}>
2022-10-25 15:36:32 +00:00
<span onClick={toggle}>{props.trigger}</span>
<Show when={isVisible()}>
<div
class={clsx(styles.popup, {
[styles.horizontalAnchorCenter]: horizontalAnchor === 'center',
2022-12-17 03:27:00 +00:00
[styles.horizontalAnchorRight]: horizontalAnchor === 'right',
[styles.bordered]: props.variant === 'bordered',
[styles.tiny]: props.variant === 'tiny'
})}
>
{props.children}
</div>
2022-10-25 15:36:32 +00:00
</Show>
</span>
2022-10-17 20:53:04 +00:00
)
}