-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9a496ec
commit a2674f8
Showing
8 changed files
with
136 additions
and
20 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import { useState } from "react"; | ||
import type { Meta, StoryObj } from "@storybook/react"; | ||
import { fn } from "@storybook/test"; | ||
|
||
import { RenamePopover } from "@swy/notion"; | ||
import { Button } from "@swy/ui/shadcn"; | ||
|
||
const meta = { | ||
title: "notion/Rename Form", | ||
component: RenamePopover, | ||
args: { | ||
onChange: fn(), | ||
}, | ||
} satisfies Meta<typeof RenamePopover>; | ||
export default meta; | ||
|
||
type Story = StoryObj<typeof meta>; | ||
|
||
const Template: Story["render"] = ({ children, ...props }) => { | ||
const [icon, setIcon] = useState(props.icon); | ||
const [title, setTitle] = useState(props.title); | ||
return ( | ||
<RenamePopover | ||
title={title} | ||
icon={icon} | ||
onChange={({ title, icon }) => { | ||
setTitle(title); | ||
setIcon(icon); | ||
}} | ||
> | ||
{children} | ||
</RenamePopover> | ||
); | ||
}; | ||
|
||
export const Default: Story = { | ||
args: { | ||
title: "My workspace", | ||
icon: { type: "lucide", name: "currency" }, | ||
children: <Button size="sm">Rename</Button>, | ||
}, | ||
render: Template, | ||
}; |
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 |
---|---|---|
@@ -1,3 +1,4 @@ | ||
export * from "./base-modal"; | ||
export * as Icon from "./icons"; | ||
export * from "./rename-popover"; | ||
export * from "./utils"; |
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,66 @@ | ||
"use client"; | ||
|
||
import React, { useRef, useState } from "react"; | ||
|
||
import { Input, Popover, PopoverContent, PopoverTrigger } from "@swy/ui/shadcn"; | ||
import { IconBlock, IconInfo, IconMenu } from "@swy/ui/shared"; | ||
|
||
interface RenamePopoverProps extends React.PropsWithChildren { | ||
title: string; | ||
icon: IconInfo; | ||
onChange: (value: { title: string; icon: IconInfo }) => void; | ||
} | ||
|
||
export const RenamePopover: React.FC<RenamePopoverProps> = ({ | ||
children, | ||
title, | ||
icon, | ||
onChange, | ||
}) => { | ||
const inputRef = useRef<HTMLInputElement>(null); | ||
|
||
const [open, setOpen] = useState(false); | ||
const onOpenChange = (isOpen: boolean) => { | ||
setOpen(isOpen); | ||
if (isOpen) | ||
setTimeout(() => { | ||
inputRef.current?.focus(); | ||
inputRef.current?.select(); | ||
}, 0); | ||
}; | ||
const onKeyDown = (e: React.KeyboardEvent<HTMLInputElement>) => { | ||
e.stopPropagation(); | ||
if (e.key === "Enter") { | ||
const value = inputRef.current?.value; | ||
if (value && value !== title) onChange({ title: value, icon }); | ||
setOpen(false); | ||
} | ||
}; | ||
|
||
return ( | ||
<Popover open={open} onOpenChange={onOpenChange}> | ||
<PopoverTrigger asChild>{children}</PopoverTrigger> | ||
<PopoverContent | ||
className="flex w-[380px] items-center gap-1.5 px-2 py-1" | ||
onClick={(e) => e.stopPropagation()} | ||
> | ||
<IconMenu | ||
className="size-7 shrink-0 border border-border-button hover:bg-primary/5" | ||
onSelect={(icon) => onChange({ title, icon })} | ||
onRemove={() => | ||
onChange({ title, icon: { type: "lucide", name: "file" } }) | ||
} | ||
> | ||
<IconBlock icon={icon} className="p-0 text-lg/[22px]" /> | ||
</IconMenu> | ||
<Input | ||
ref={inputRef} | ||
className="whitespace-pre-wrap break-words" | ||
value={title} | ||
onChange={(e) => onChange({ title: e.target.value, icon })} | ||
onKeyDown={onKeyDown} | ||
/> | ||
</PopoverContent> | ||
</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
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
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