Skip to content

Commit e4c3867

Browse files
committed
add SfDrawer
1 parent dae3af8 commit e4c3867

File tree

3 files changed

+71
-0
lines changed

3 files changed

+71
-0
lines changed
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import { $, Slot, component$, useSignal } from '@builder.io/qwik';
2+
import { SfDrawerPlacement } from '../../shared/SfDrawer';
3+
import { SfDrawerProps } from './types';
4+
5+
const defaultDrawerTag = 'aside';
6+
7+
const placementClasses = (placementValue: SfDrawerProps['placement']) => `
8+
${placementValue !== SfDrawerPlacement.right ? 'left-0' : ''}
9+
${placementValue !== SfDrawerPlacement.left ? 'right-0' : ''}
10+
${placementValue !== SfDrawerPlacement.bottom ? 'top-0' : ''}
11+
${placementValue !== SfDrawerPlacement.top ? 'bottom-0' : ''}
12+
`;
13+
14+
export const SfDrawer = component$<SfDrawerProps>(
15+
({
16+
as,
17+
ref,
18+
open,
19+
placement = SfDrawerPlacement.left,
20+
disableClickAway,
21+
disableEsc,
22+
onClose$,
23+
class: _class,
24+
...attributes
25+
}) => {
26+
const Tag = as || defaultDrawerTag;
27+
const drawerRef = useSignal<Element>();
28+
29+
// TODO
30+
// useClickAway(drawerRef, () => {
31+
// if (disableClickAway) return;
32+
// onClose?.();
33+
// });
34+
35+
const onKeyDown = $((event: KeyboardEvent) => {
36+
if (disableEsc) return;
37+
if (event.key === 'Escape') {
38+
onClose$?.();
39+
}
40+
});
41+
42+
return open ? (
43+
<Tag
44+
ref={ref ? ref : drawerRef}
45+
className={`fixed ${placementClasses(placement)} ${_class}`}
46+
tabIndex="-1"
47+
data-testid="drawer"
48+
{...attributes}
49+
onKeyDown$={onKeyDown}
50+
>
51+
<Slot />
52+
</Tag>
53+
) : null;
54+
}
55+
);
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 { SfDrawer } from './SfDrawer';
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { Signal } from '@builder.io/qwik';
2+
import { SfDrawerPlacement } from '../../shared/SfDrawer';
3+
4+
export type SfDrawerProps = {
5+
as?: any;
6+
ref?: Signal<Element | undefined>;
7+
open: boolean;
8+
class: string;
9+
placement?: `${SfDrawerPlacement}`;
10+
disableClickAway?: boolean;
11+
disableEsc?: boolean;
12+
onClose$?: () => void;
13+
};

0 commit comments

Comments
 (0)