-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[WEB-1878] ui: implementing popover component in a UI package (#5063)
* ui: impleented popover commponent * chore: implemente component in project-states * chore: added default styling for popover menu panel * chore: removed propsWithChildren in popover component type
- Loading branch information
1 parent
6dcbea6
commit fc2585b
Showing
11 changed files
with
258 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * from "./popover"; | ||
export * from "./popover-menu"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import React from "react"; | ||
import type { Meta, StoryObj } from "@storybook/react"; | ||
import { PopoverMenu } from "./popover-menu"; | ||
|
||
const meta: Meta<typeof PopoverMenu> = { | ||
title: "PopoverMenu", | ||
component: PopoverMenu, | ||
}; | ||
|
||
export default meta; | ||
|
||
// types | ||
type TPopoverMenu = { | ||
id: number; | ||
name: string; | ||
}; | ||
|
||
type Story = StoryObj<typeof PopoverMenu<TPopoverMenu>>; | ||
|
||
// data | ||
const data: TPopoverMenu[] = [ | ||
{ id: 1, name: "John Doe" }, | ||
{ id: 2, name: "Jane Doe" }, | ||
{ id: 3, name: "John Smith" }, | ||
{ id: 4, name: "Jane Smith" }, | ||
]; | ||
|
||
// components | ||
const PopoverMenuItemRender = (item: TPopoverMenu) => ( | ||
<div className="text-sm text-gray-600 hover:text-gray-700 rounded-sm cursor-pointer hover:bg-gray-200 transition-all px-1.5 py-0.5 capitalize"> | ||
{item.name} | ||
</div> | ||
); | ||
|
||
// stories | ||
export const Default: Story = { | ||
args: { | ||
popperPosition: "bottom-start", | ||
panelClassName: "rounded bg-gray-100 p-2", | ||
data: data, | ||
keyExtractor: (item, index: number) => `${item.id}-${index}`, | ||
render: (item) => PopoverMenuItemRender(item), | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import React, { Fragment } from "react"; | ||
// components | ||
import { Popover } from "./popover"; | ||
// helpers | ||
import { cn } from "../../helpers"; | ||
// types | ||
import { TPopoverMenu } from "./types"; | ||
|
||
export const PopoverMenu = <T,>(props: TPopoverMenu<T>) => { | ||
const { | ||
popperPosition = "bottom-end", | ||
popperPadding = 0, | ||
buttonClassName = "", | ||
button, | ||
panelClassName = "", | ||
data, | ||
keyExtractor, | ||
render, | ||
} = props; | ||
|
||
return ( | ||
<Popover | ||
popperPosition={popperPosition} | ||
popperPadding={popperPadding} | ||
buttonClassName={buttonClassName} | ||
button={button} | ||
panelClassName={cn( | ||
"my-1 w-48 rounded border-[0.5px] border-custom-border-300 bg-custom-background-100 px-2 py-2 text-xs shadow-custom-shadow-rg focus:outline-none", | ||
panelClassName | ||
)} | ||
> | ||
<Fragment> | ||
{data.map((item, index) => ( | ||
<Fragment key={keyExtractor(item, index)}>{render(item, index)}</Fragment> | ||
))} | ||
</Fragment> | ||
</Popover> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import type { Meta, StoryObj } from "@storybook/react"; | ||
import React from "react"; | ||
import { Popover } from "./popover"; | ||
|
||
const meta: Meta<typeof Popover> = { | ||
title: "Popover", | ||
component: Popover, | ||
}; | ||
|
||
export default meta; | ||
|
||
// types | ||
type Story = StoryObj<typeof Popover>; | ||
|
||
// data | ||
|
||
// components | ||
const RenderCustomPopoverComponent = ( | ||
<div className="space-y-2"> | ||
<div className="text-sm font-medium text-gray-500">Your custom component</div> | ||
<div> | ||
{["option1", "option2", "option3"].map((option) => ( | ||
<div | ||
key={option} | ||
className="text-sm text-gray-600 hover:text-gray-700 rounded-sm cursor-pointer hover:bg-gray-200 transition-all px-1.5 py-0.5 capitalize" | ||
> | ||
{option} | ||
</div> | ||
))} | ||
</div> | ||
</div> | ||
); | ||
|
||
// stories | ||
export const Default: Story = { | ||
args: { | ||
popperPosition: "bottom-start", | ||
panelClassName: "rounded bg-gray-100 p-2", | ||
children: RenderCustomPopoverComponent, | ||
}, | ||
}; | ||
|
||
export const CustomMenuButton: Story = { | ||
args: { | ||
popperPosition: "bottom-start", | ||
button: ( | ||
<div className="p-2 text-sm font-medium rounded bg-gray-100 hover:bg-gray-200 transition-all"> | ||
Custom Menu Button | ||
</div> | ||
), | ||
panelClassName: "rounded bg-gray-100 p-2", | ||
children: RenderCustomPopoverComponent, | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
import React, { Fragment, useState } from "react"; | ||
import { usePopper } from "react-popper"; | ||
import { Popover as HeadlessReactPopover, Transition } from "@headlessui/react"; | ||
// helpers | ||
import { cn } from "../../helpers"; | ||
// types | ||
import { TPopover } from "./types"; | ||
import { EllipsisVertical } from "lucide-react"; | ||
|
||
export const Popover = (props: TPopover) => { | ||
const { | ||
popperPosition = "bottom-end", | ||
popperPadding = 0, | ||
buttonClassName = "", | ||
button, | ||
panelClassName = "", | ||
children, | ||
} = props; | ||
// states | ||
const [referenceElement, setReferenceElement] = useState<HTMLButtonElement | null>(null); | ||
const [popperElement, setPopperElement] = useState<HTMLDivElement | null>(null); | ||
|
||
// react-popper derived values | ||
const { styles, attributes } = usePopper(referenceElement, popperElement, { | ||
placement: popperPosition, | ||
modifiers: [ | ||
{ | ||
name: "preventOverflow", | ||
options: { | ||
padding: popperPadding, | ||
}, | ||
}, | ||
], | ||
}); | ||
|
||
return ( | ||
<HeadlessReactPopover className="relative flex h-full w-full items-center justify-center"> | ||
<HeadlessReactPopover.Button ref={setReferenceElement} className="flex justify-center items-center"> | ||
{button ? ( | ||
button | ||
) : ( | ||
<div | ||
className={cn( | ||
"flex justify-center items-center text-base h-6 w-6 rounded transition-all bg-custom-background-90 hover:bg-custom-background-80", | ||
buttonClassName | ||
)} | ||
> | ||
<EllipsisVertical className="h-3 w-3" /> | ||
</div> | ||
)} | ||
</HeadlessReactPopover.Button> | ||
|
||
<Transition | ||
as={Fragment} | ||
enter="transition ease-out duration-200" | ||
enterFrom="opacity-0 translate-y-1" | ||
enterTo="opacity-100 translate-y-0" | ||
leave="transition ease-in duration-150" | ||
leaveFrom="opacity-100 translate-y-0" | ||
leaveTo="opacity-0 translate-y-1" | ||
> | ||
<HeadlessReactPopover.Panel | ||
ref={setPopperElement} | ||
style={styles.popper} | ||
{...attributes.popper} | ||
className={cn("absolute left-0 top-full z-20 w-screen max-w-xs mt-2", panelClassName)} | ||
> | ||
{children} | ||
</HeadlessReactPopover.Panel> | ||
</Transition> | ||
</HeadlessReactPopover> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import { ReactNode } from "react"; | ||
import { Placement } from "@popperjs/core"; | ||
|
||
export type TPopoverButtonDefaultOptions = { | ||
// button and button styling | ||
button?: ReactNode; | ||
buttonClassName?: string; | ||
}; | ||
|
||
export type TPopoverDefaultOptions = TPopoverButtonDefaultOptions & { | ||
// popper styling | ||
popperPosition?: Placement | undefined; | ||
popperPadding?: number | undefined; | ||
// panel styling | ||
panelClassName?: string; | ||
}; | ||
|
||
export type TPopover = TPopoverDefaultOptions & { | ||
// children | ||
children: ReactNode; | ||
}; | ||
|
||
export type TPopoverMenu<T> = TPopoverDefaultOptions & { | ||
data: T[]; | ||
keyExtractor: (item: T, index: number) => string; | ||
render: (item: T, index: number) => ReactNode; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters