Skip to content

Commit 884b739

Browse files
committed
add SfModal
1 parent 04dbe1d commit 884b739

File tree

3 files changed

+62
-0
lines changed

3 files changed

+62
-0
lines changed
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import { Slot, component$ } from '@builder.io/qwik';
2+
import { SfModalProps } from './types';
3+
4+
const defaultModalTag = 'div';
5+
6+
export const SfModal = component$<SfModalProps>(
7+
({ as, ref, open, disableClickAway, disableEsc, onClose, class: _class, ...attributes }) => {
8+
const Tag = as || defaultModalTag;
9+
10+
// TODO
11+
// const modalRef = useRef(null);
12+
// useClickAway(modalRef, () => {
13+
// if (disableClickAway) return;
14+
// onClose?.();
15+
// });
16+
17+
const onKeyDown = (event: KeyboardEvent) => {
18+
if (!disableEsc && event.key === 'Escape') {
19+
onClose?.();
20+
}
21+
if ('onKeyDown' in attributes && typeof attributes.onKeyDown === 'function') return attributes.onKeyDown?.(event);
22+
};
23+
24+
// TODO
25+
// useTrapFocus(modalRef, {
26+
// activeState: open,
27+
// initialFocus: InitialFocusType.autofocus,
28+
// initialFocusContainerFallback: true,
29+
// });
30+
31+
return open ? (
32+
<Tag
33+
{...(ref ? { ref } : {})}
34+
className={`fixed inset-0 w-fit h-fit m-auto p-6 pt-10 lg:p-10 border border-neutral-100 bg-white shadow-xl rounded-xl outline-none
35+
${_class}`}
36+
tabIndex="-1"
37+
aria-modal="true"
38+
data-testid="modal"
39+
{...attributes}
40+
onKeyDown={onKeyDown}
41+
>
42+
<Slot />
43+
</Tag>
44+
) : null;
45+
},
46+
);
47+
48+
export default SfModal;
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export * from './types';
2+
3+
export { SfModal } from './SfModal';
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { PropFunction, QwikIntrinsicElements, Signal } from '@builder.io/qwik';
2+
3+
export type SfModalProps = QwikIntrinsicElements['modal'] & {
4+
as: any;
5+
class?: string;
6+
ref?: Signal<Element>
7+
open?: boolean;
8+
disableClickAway?: boolean;
9+
disableEsc?: boolean;
10+
onClose$?: PropFunction<() => void>;
11+
};

0 commit comments

Comments
 (0)