Skip to content

Commit

Permalink
Merge pull request #1851 from dev-launchers/development/components
Browse files Browse the repository at this point in the history
feat(components): Popover component (#1723)
  • Loading branch information
Enjoy2Live authored Jul 10, 2024
2 parents d3fe0e5 + ff54165 commit 37637f8
Show file tree
Hide file tree
Showing 6 changed files with 253 additions and 0 deletions.
1 change: 1 addition & 0 deletions packages/UI/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"@radix-ui/react-checkbox": "1.0.4",
"@radix-ui/react-dialog": "1.0.5",
"@radix-ui/react-dropdown-menu": "2.0.6",
"@radix-ui/react-popover": "1.0.7",
"@radix-ui/react-progress": "1.0.3",
"@radix-ui/react-switch": "1.0.3",
"@radix-ui/react-tabs": "1.0.4",
Expand Down
72 changes: 72 additions & 0 deletions packages/UI/src/components/Popover/Popover.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import {
Meta,
Controls,
Stories,
Description,
Primary,
} from '@storybook/blocks';
import * as PopoverStories from './Popover.stories.tsx';
import Popover from './Popover.tsx';

# Popover

Popover component. HasCloseBtn will automatically add a button, if developers need another way to close the popover, you can use PopoverClose. Everything needs to be wrapped in the `Popover` component.

## Anatomy

```jsx
import {
Popover,
PopoverContent,
PopoverTrigger,
PopoverClose,
} from '@devlaunchers/components/src/components/Popover';

export default () => (
<Popover>
<PopoverTrigger />
<PopoverContent>
<PopoverClose />
</PopoverContent>
</Popover>
);
```

### Popover

Popover is the main component that will wrap the PopoverTrigger and PopoverContent.

### PopoverTrigger

PopoverTrigger is a wrapper component that will be displayed and when clicked it will open the PopoverContent.
PopoverTrigger is an button component, if you want to use your own button make sure you are passing `asChild` prop.

### PopoverContent

PopoverContent is a wrapper that will hide the content until the PopoverTrigger is clicked on.

### PopoverClose & Close buttons.

#### Built in close button.

If you want too have automatically add a close X button, pass the prop hasCloseBtn = true.

#### Adding a dynamic close button.

If you want to a custom close button, use PopoverClose as a wrapper for the component you want to close the popover.

## Examples

### [Constrain the content size](https://www.radix-ui.com/primitives/docs/components/popover#constrain-the-content-size)

<p className="!mt-0 !text-base">
You may want to constrain the width of the content so that it matches the
trigger width. You may also want to constrain its height to not exceed the
viewport.
</p>

<Meta of={PopoverStories} />

<Description of={PopoverStories} />

<Stories />
73 changes: 73 additions & 0 deletions packages/UI/src/components/Popover/Popover.stories.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import type { Meta, StoryObj } from '@storybook/react';
import Button from '../atoms/Button';
import {
Popover,
PopoverContent,
PopoverTrigger,
PopoverClose,
} from './Popover';

const meta: Meta<typeof Popover> = {
component: Popover,
};

export default meta;
type Story = StoryObj<typeof Popover>;

/*
*👇 Render functions are a framework specific feature to allow you control on how the component renders.
* See https://storybook.js.org/docs/react/api/csf
* to learn how to use render functions.
*/
export const Primary: Story = {
render: () => (
<Popover>
<PopoverTrigger asChild>
<Button buttonType="primary" buttonSize="standard">
Open
</Button>
</PopoverTrigger>
<PopoverContent hasCloseBtn={false}>
{' '}
Click outside of the popover to close
</PopoverContent>
</Popover>
),
};

export const WithClose: Story = {
render: () => (
<Popover>
<PopoverTrigger asChild>
<Button buttonType="primary" buttonSize="standard">
Open
</Button>
</PopoverTrigger>
<PopoverContent className="w-96"> Click X to close </PopoverContent>
</Popover>
),
};

export const UsingPopoverClose: Story = {
render: () => (
<Popover>
<PopoverTrigger asChild>
<Button buttonType="primary" buttonSize="standard">
Open
</Button>
</PopoverTrigger>
<PopoverContent
hasCloseBtn={false}
className="flex w-96 items-center justify-between"
>
<p>Click button to close</p>
<PopoverClose>
<Button buttonType="primary" buttonSize="standard">
{' '}
Close{' '}
</Button>
</PopoverClose>
</PopoverContent>
</Popover>
),
};
66 changes: 66 additions & 0 deletions packages/UI/src/components/Popover/Popover.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
'use client';
import * as PopoverPrimitive from '@radix-ui/react-popover';
import { X } from 'lucide-react';
import * as React from 'react';
import { cn } from '../../utils/classesMerger';

/**
* @see http://localhost:6006/?path=/docs/components-popover--docs
*/

const Popover = PopoverPrimitive.Root;
const PopoverTrigger = PopoverPrimitive.Trigger;
const PopoverAnchor = PopoverPrimitive.Anchor;
const PopoverClose = PopoverPrimitive.Close;

const PopoverContent = React.forwardRef<
React.ElementRef<typeof PopoverPrimitive.Content>,
Omit<
React.ComponentPropsWithoutRef<typeof PopoverPrimitive.Content>,
'align'
> & {
/**
* Boolean when set to false will remove the close button in the top right corner of the dialog.
*/
hasCloseBtn?: boolean;
/**
* The side offset of the dialog.
*/
align?: 'center' | 'start' | 'end';
}
>(
(
{
className,
children,
align = 'center',
hasCloseBtn = true,
sideOffset = 4,
...props
},
ref
) => (
<PopoverPrimitive.Portal>
<PopoverPrimitive.Content
ref={ref}
align={align}
sideOffset={sideOffset}
className={cn(
'z-50 rounded-md border bg-white p-8 text-popover-foreground shadow-md outline-none data-open:animate-in data-closed:animate-out data-closed:fade-out-0 data-open:fade-in-0 data-closed:zoom-out-95 data-open:zoom-in-95',
className
)}
{...props}
>
{children}
{hasCloseBtn && (
<PopoverPrimitive.Close className="absolute right-8 top-8 flex items-center justify-center rounded-md bg-white drop-shadow-xl">
<X className="h-6 w-6" />
</PopoverPrimitive.Close>
)}
</PopoverPrimitive.Content>
</PopoverPrimitive.Portal>
)
);
PopoverContent.displayName = PopoverPrimitive.Content.displayName;

export { Popover, PopoverTrigger, PopoverContent, PopoverAnchor, PopoverClose };
6 changes: 6 additions & 0 deletions packages/UI/src/components/Popover/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export {
Popover,
PopoverTrigger,
PopoverContent,
PopoverAnchor,
} from './Popover';
35 changes: 35 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2683,6 +2683,7 @@ __metadata:
"@radix-ui/react-checkbox": 1.0.4
"@radix-ui/react-dialog": 1.0.5
"@radix-ui/react-dropdown-menu": 2.0.6
"@radix-ui/react-popover": 1.0.7
"@radix-ui/react-progress": 1.0.3
"@radix-ui/react-switch": 1.0.3
"@radix-ui/react-tabs": 1.0.4
Expand Down Expand Up @@ -6413,6 +6414,40 @@ __metadata:
languageName: node
linkType: hard

"@radix-ui/react-popover@npm:1.0.7":
version: 1.0.7
resolution: "@radix-ui/react-popover@npm:1.0.7"
dependencies:
"@babel/runtime": ^7.13.10
"@radix-ui/primitive": 1.0.1
"@radix-ui/react-compose-refs": 1.0.1
"@radix-ui/react-context": 1.0.1
"@radix-ui/react-dismissable-layer": 1.0.5
"@radix-ui/react-focus-guards": 1.0.1
"@radix-ui/react-focus-scope": 1.0.4
"@radix-ui/react-id": 1.0.1
"@radix-ui/react-popper": 1.1.3
"@radix-ui/react-portal": 1.0.4
"@radix-ui/react-presence": 1.0.1
"@radix-ui/react-primitive": 1.0.3
"@radix-ui/react-slot": 1.0.2
"@radix-ui/react-use-controllable-state": 1.0.1
aria-hidden: ^1.1.1
react-remove-scroll: 2.5.5
peerDependencies:
"@types/react": "*"
"@types/react-dom": "*"
react: ^16.8 || ^17.0 || ^18.0
react-dom: ^16.8 || ^17.0 || ^18.0
peerDependenciesMeta:
"@types/react":
optional: true
"@types/react-dom":
optional: true
checksum: 3ec15c0923ea457f586aa34f77e17fabffa02dffeab622951560ec21c38df2f43718ff088d24bf9fd1d9cd0db62436fc19cae5b122d90f72de4945a1f508dc59
languageName: node
linkType: hard

"@radix-ui/react-popper@npm:1.1.2":
version: 1.1.2
resolution: "@radix-ui/react-popper@npm:1.1.2"
Expand Down

0 comments on commit 37637f8

Please sign in to comment.