Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions src/components/BoemlyModal/BoemlyModal.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,20 @@ export default {
options: ['xs', 'sm', 'md', 'lg', 'xl', 'full', 'cover'],
control: { type: 'radio' },
},
position: {
options: [
'topLeft',
'top',
'topRight',
'left',
'center',
'right',
'bottomLeft',
'bottom',
'bottomRight',
],
control: { type: 'select' },
},
},
} as Meta<typeof BoemlyModal>;

Expand Down Expand Up @@ -70,6 +84,15 @@ Size.args = {
size: 'xl',
};

export const Position = Template.bind({});
Position.args = {
title: 'Centered Modal',
content: <div>Use the `position` prop to align the modal within the viewport.</div>,
footer: <Button variant="plain">Button</Button>,
size: 'md',
position: 'center',
};

export const WithCustomTitle = Template.bind({});
WithCustomTitle.args = {
title: (
Expand Down
60 changes: 58 additions & 2 deletions src/components/BoemlyModal/BoemlyModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,16 @@ export interface BoemlyModalProps {
trigger: ReactNode;
footer?: ReactNode | ((props: { onClose: () => void }) => ReactNode);
size?: 'xs' | 'sm' | 'md' | 'lg' | 'xl' | 'full' | 'cover';
position?:
| 'topLeft'
| 'top'
| 'topRight'
| 'left'
| 'center'
| 'right'
| 'bottomLeft'
| 'bottom'
| 'bottomRight';
open: boolean;
onOpenChange: (open: boolean) => void;
}
Expand All @@ -33,11 +43,57 @@ export const BoemlyModal: React.FC<BoemlyModalProps> = ({
trigger,
footer,
size = 'xl',
position = 'center',
open,
onOpenChange,
}: BoemlyModalProps) => {
const onClose = () => onOpenChange(false);

const positionStyles: Record<
NonNullable<BoemlyModalProps['position']>,
{
positioner: { justifyContent: string; alignItems: string };
content: { marginInlineStart: string; marginInlineEnd: string };
}
> = {
topLeft: {
positioner: { justifyContent: 'flex-start', alignItems: 'flex-start' },
content: { marginInlineStart: '0', marginInlineEnd: 'auto' },
},
top: {
positioner: { justifyContent: 'center', alignItems: 'flex-start' },
content: { marginInlineStart: 'auto', marginInlineEnd: 'auto' },
},
topRight: {
positioner: { justifyContent: 'flex-end', alignItems: 'flex-start' },
content: { marginInlineStart: 'auto', marginInlineEnd: '0' },
},
left: {
positioner: { justifyContent: 'flex-start', alignItems: 'center' },
content: { marginInlineStart: '0', marginInlineEnd: 'auto' },
},
center: {
positioner: { justifyContent: 'center', alignItems: 'center' },
content: { marginInlineStart: 'auto', marginInlineEnd: 'auto' },
},
right: {
positioner: { justifyContent: 'flex-end', alignItems: 'center' },
content: { marginInlineStart: 'auto', marginInlineEnd: '0' },
},
bottomLeft: {
positioner: { justifyContent: 'flex-start', alignItems: 'flex-end' },
content: { marginInlineStart: '0', marginInlineEnd: 'auto' },
},
bottom: {
positioner: { justifyContent: 'center', alignItems: 'flex-end' },
content: { marginInlineStart: 'auto', marginInlineEnd: 'auto' },
},
bottomRight: {
positioner: { justifyContent: 'flex-end', alignItems: 'flex-end' },
content: { marginInlineStart: 'auto', marginInlineEnd: '0' },
},
};

const renderFooter = () => {
if (!footer) return null;

Expand All @@ -55,8 +111,8 @@ export const BoemlyModal: React.FC<BoemlyModalProps> = ({
<Dialog.Trigger asChild>{trigger}</Dialog.Trigger>
<Portal>
<Dialog.Backdrop />
<Dialog.Positioner>
<Dialog.Content>
<Dialog.Positioner {...positionStyles[position].positioner}>
<Dialog.Content {...positionStyles[position].content}>
<Dialog.Header>
<Dialog.Title>{title}</Dialog.Title>
</Dialog.Header>
Expand Down
Loading