Skip to content

Commit

Permalink
feat: Dialog/modal
Browse files Browse the repository at this point in the history
  • Loading branch information
shindeajinkya committed Aug 28, 2022
1 parent 9973c41 commit 6fd4953
Show file tree
Hide file tree
Showing 4 changed files with 118 additions and 0 deletions.
31 changes: 31 additions & 0 deletions packages/components/src/elements/Dialog/Dialog.stories.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import {
Dialog,
DialogContent,
DialogDescription,
DialogTitle,
DialogTrigger,
} from './';
import { ComponentStory, ComponentMeta } from '@storybook/react';
import { Button } from '../Button';

export default {
title: 'Elements/Dialog',
component: Dialog,
argTypes: {},
} as ComponentMeta<typeof Dialog>;

const Template: ComponentStory<typeof Dialog> = (args) => (
<Dialog>
<DialogTrigger asChild>
<Button>Edit profile</Button>
</DialogTrigger>
<DialogContent>
<DialogTitle>Edit profile</DialogTitle>
<DialogDescription>
Make changes to your profile here. Click save when you're done.
</DialogDescription>
</DialogContent>
</Dialog>
);

export const Default = Template.bind({});
10 changes: 10 additions & 0 deletions packages/components/src/elements/Dialog/Dialog.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { render } from '@testing-library/react';

import { Dialog } from './Dialog';

describe('Dialog', () => {
it('renders without throwing', () => {
const { container } = render(<Dialog />);
expect(container).toBeInTheDocument();
});
});
76 changes: 76 additions & 0 deletions packages/components/src/elements/Dialog/Dialog.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import React from 'react';
import * as DialogPrimitive from '@radix-ui/react-dialog';
import { Cross1Icon } from '@radix-ui/react-icons';
// import { IconButton } from './IconButton';
import { CSS, styled } from '../../theme';
import { overlayStyles, panelStyles } from '../../common';

const Dialog = DialogPrimitive.Root;
const DialogTrigger = DialogPrimitive.Trigger;

const StyledOverlay = styled(DialogPrimitive.Overlay, overlayStyles, {
position: 'fixed',
top: 0,
right: 0,
bottom: 0,
left: 0,
});

const StyledContent = styled(DialogPrimitive.Content, panelStyles, {
position: 'fixed',
top: '50%',
left: '50%',
transform: 'translate(-50%, -50%)',
minWidth: 200,
maxHeight: '85vh',
padding: '$4',
marginTop: '-5vh',
// animation: `${fadeIn} 125ms linear, ${moveDown} 125ms cubic-bezier(0.22, 1, 0.36, 1)`,

// Among other things, prevents text alignment inconsistencies when dialog can't be centered in the viewport evenly.
// Affects animated and non-animated dialogs alike.
willChange: 'transform',

'&:focus': {
outline: 'none',
},
});

const StyledCloseButton = styled(DialogPrimitive.Close, {
position: 'absolute',
top: '$2',
right: '$2',
});

type DialogContentPrimitiveProps = React.ComponentProps<
typeof DialogPrimitive.Content
>;
type DialogContentProps = DialogContentPrimitiveProps & { css?: CSS };

const DialogContent = React.forwardRef<
React.ElementRef<typeof StyledContent>,
DialogContentProps
>(({ children, ...props }, forwardedRef) => (
<DialogPrimitive.Portal>
<StyledOverlay />
<StyledContent {...props} ref={forwardedRef}>
{children}
<StyledCloseButton asChild>
<Cross1Icon />
</StyledCloseButton>
</StyledContent>
</DialogPrimitive.Portal>
));

const DialogClose = DialogPrimitive.Close;
const DialogTitle = DialogPrimitive.Title;
const DialogDescription = DialogPrimitive.Description;

export {
Dialog,
DialogTrigger,
DialogContent,
DialogClose,
DialogTitle,
DialogDescription,
};
1 change: 1 addition & 0 deletions packages/components/src/elements/Dialog/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './Dialog';

0 comments on commit 6fd4953

Please sign in to comment.