-
Notifications
You must be signed in to change notification settings - Fork 2
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
Showing
17 changed files
with
940 additions
and
23 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
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,14 @@ | ||
import {PropsWithChildren, useEffect, useState} from "react"; | ||
import {useIsClient} from "usehooks-ts"; | ||
|
||
const ClientOnly = (props: PropsWithChildren) => { | ||
const isClient = useIsClient(); | ||
|
||
if (!isClient) { | ||
return false; | ||
} | ||
|
||
return props.children; | ||
}; | ||
|
||
export default ClientOnly; |
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,10 @@ | ||
import {createPortal} from "react-dom"; | ||
import {PropsWithChildren} from "react"; | ||
const Portal = (props: PropsWithChildren<{id: string}>) => { | ||
return createPortal( | ||
props.children, | ||
document.getElementById(props.id)! | ||
); | ||
}; | ||
|
||
export default Portal; |
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,28 @@ | ||
import {cx} from "@emotion/css"; | ||
import rootStackingContext from "@nextutils/ui/rootStackingContext"; | ||
import React from "react"; | ||
|
||
const ActionSheetBackdrop = (props: { | ||
isOpen: boolean, | ||
onBackdropClick?: () => void, | ||
children?: React.ReactNode; | ||
}) => { | ||
return (<div className={cx( | ||
"fixed inset-0", | ||
rootStackingContext.actionSheetBackdrop, | ||
"flex items-center justify-center", | ||
"transition overflow-hidden", | ||
props.isOpen ? "bg-black/60" : "bg-black/0", | ||
props.isOpen ? "" : "hidden", | ||
)} onClick={(e) => { | ||
// only when clicking on the backdrop itself | ||
// https://stackoverflow.com/a/47406614/1922857 | ||
if (e.target === e.currentTarget) { | ||
props.onBackdropClick?.(); | ||
} | ||
}}> | ||
{props.children} | ||
</div>); | ||
}; | ||
|
||
export default ActionSheetBackdrop; |
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,121 @@ | ||
import {Menu, MenuDivider, MenuItem, SubMenu} from "@szhsin/react-menu"; | ||
import React, {ReactNode} from "react"; | ||
import useIsMobileWidth from "@nextutils/ui/useIsMobileWidth"; | ||
import useActionSheetStore from "@nextutils/ui/actionSheet/useActionSheetStore"; | ||
import ActionSheetWrapper from "@nextutils/ui/actionSheet/ActionSheetWrapper"; | ||
import MobileStackRow from "@slidde/editor2/MobileStackRow"; | ||
|
||
export type ActionSheetMenuItem = { | ||
custom: ReactNode; | ||
} | { | ||
label?: ReactNode; | ||
icon?: ReactNode; | ||
href?: string; | ||
onClick?: () => void; | ||
|
||
items?: ActionSheetMenuItem[]; | ||
} | 'divider'; | ||
|
||
const ActionSheetMenuItem = (props: {item: ActionSheetMenuItem, depth: number}) => { | ||
const {item, depth} = props; | ||
if (item === 'divider') { | ||
return <div className="my-2 h-[1px] bg-slate-300" />; | ||
} | ||
if ('custom' in item) { | ||
return <div className="my-2">{item.custom}</div>; | ||
} | ||
|
||
const {items} = item; | ||
if (items) { | ||
return <MobileStackRow | ||
{...item} | ||
onClick={() => { | ||
useActionSheetStore.getState().push( | ||
<ActionSheetWrapper> | ||
{items.map((item, index) => { | ||
return <ActionSheetMenuItem key={index} item={item} depth={depth + 1} />; | ||
})} | ||
</ActionSheetWrapper> | ||
) | ||
}} | ||
/>; | ||
} | ||
|
||
return <MobileStackRow | ||
{...item} | ||
onClick={() => { | ||
// leaf level, do close | ||
// do it before onClick, since the onClick may trigger a new action sheet | ||
useActionSheetStore.getState().pop(depth); | ||
|
||
item.onClick?.(); | ||
}} | ||
/>; | ||
} | ||
|
||
const ActionSheetMenu = (props: { | ||
button: ReactNode, | ||
buttonClassName?: string, | ||
items: ActionSheetMenuItem[], | ||
}) => { | ||
const isMobileWidth = useIsMobileWidth(); | ||
|
||
if (isMobileWidth) { | ||
return <div className={props.buttonClassName} onClick={() => { | ||
useActionSheetStore.getState().push( | ||
<ActionSheetWrapper> | ||
{props.items.map((item, index) => { | ||
return <ActionSheetMenuItem key={index} item={item} depth={1} />; | ||
})} | ||
</ActionSheetWrapper> | ||
); | ||
}}> | ||
{props.button} | ||
</div> | ||
} | ||
|
||
return <Menu menuButton={(modifiers) => <div className={props.buttonClassName}>{props.button}</div>}> | ||
{props.items.map((item, index) => { | ||
if (item === 'divider') { | ||
return <MenuDivider key={index} />; | ||
} | ||
if ('custom' in item) { | ||
return <div key={index} className="max-w-[20rem] px-2">{item.custom}</div>; | ||
} | ||
|
||
if (item.items) { | ||
return <SubMenu key={index} label={<>{item.icon} {item.label}</>}> | ||
{item.items.map((item, index) => { | ||
if (item === 'divider') { | ||
return <MenuDivider key={index} />; | ||
} | ||
|
||
if ('custom' in item) { | ||
return <div key={index} className="max-w-[20rem] px-2">{item.custom}</div>; | ||
} | ||
|
||
return <MenuItem | ||
key={index} | ||
href={item.href} | ||
onClick={item.onClick} | ||
> | ||
{item.icon} | ||
{item.label} | ||
</MenuItem>; | ||
})} | ||
</SubMenu> | ||
} | ||
|
||
return <MenuItem | ||
key={index} | ||
href={item.href} | ||
onClick={item.onClick} | ||
> | ||
{item.icon} | ||
{item.label} | ||
</MenuItem>; | ||
})} | ||
</Menu>; | ||
}; | ||
|
||
export default ActionSheetMenu; |
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,14 @@ | ||
import {cx} from "@emotion/css"; | ||
import React, {ReactNode} from "react"; | ||
|
||
const ActionSheetRenderStack = (props: {stack: ReactNode[]}) => { | ||
const actionSheetStack = props.stack; | ||
|
||
return actionSheetStack.map((node, i) => { | ||
return <React.Fragment key={i}> | ||
{node} | ||
</React.Fragment>; | ||
}); | ||
}; | ||
|
||
export default ActionSheetRenderStack; |
Oops, something went wrong.