-
Notifications
You must be signed in to change notification settings - Fork 8
feat[mobile]: make share menu nice #2781
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
peterchinman
wants to merge
11
commits into
main
Choose a base branch
from
peter/mobile-dialogs
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
72435ca
make mobile share dialog nice
peterchinman f52f334
biome
peterchinman 228cb2c
types
peterchinman 11b7f6e
Merge remote-tracking branch 'origin' into peter/mobile-dialogs
peterchinman 9159034
make work with markdown shell
peterchinman b7fea99
fixes
peterchinman d3bb8b9
get auto focus working correctly
peterchinman 365357f
fixes
peterchinman 38bcc2d
merge
peterchinman 7d3a404
fix
peterchinman 6e3429c
fix responsive dropdown for desktop
peterchinman File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,295 @@ | ||
| import { isMobile } from '@core/mobile/isMobile'; | ||
| import { isTouchDevice } from '@core/mobile/isTouchDevice'; | ||
| import { | ||
| autoUpdate, | ||
| computePosition, | ||
| flip, | ||
| offset, | ||
| shift, | ||
| } from '@floating-ui/dom'; | ||
| import { DropdownMenu } from '@kobalte/core/dropdown-menu'; | ||
| import { cn } from '@ui/utils/classname'; | ||
| import { | ||
| type Accessor, | ||
| type Component, | ||
| createContext, | ||
| createSignal, | ||
| type JSX, | ||
| onCleanup, | ||
| onMount, | ||
| Show, | ||
| splitProps, | ||
| useContext, | ||
| type ValidComponent, | ||
| } from 'solid-js'; | ||
| import { Dynamic, Portal } from 'solid-js/web'; | ||
|
|
||
| // SimpleDropdown is a hand-rolled dropdown with no focus management — Kobalte's | ||
| // DropdownMenu unconditionally restores focus to its trigger on close, which | ||
| // conflicts with MobileDrawer focus control. Use SimpleDropdown (via | ||
| // ResponsiveDropdown) on touch devices wherever you need to own focus yourself. | ||
|
|
||
| // --- Internal floating content --- | ||
|
|
||
| function FloatingContent(props: { | ||
| anchor: HTMLElement; | ||
| boundary: HTMLElement | null; | ||
| onClose: () => void; | ||
| children: JSX.Element; | ||
| }) { | ||
| let ref!: HTMLDivElement; | ||
| const [pos, setPos] = createSignal({ x: 0, y: 0 }); | ||
|
|
||
| onMount(() => { | ||
| const update = async () => { | ||
| const { x, y } = await computePosition(props.anchor, ref, { | ||
| strategy: 'fixed', | ||
| placement: 'bottom-end', | ||
| middleware: [ | ||
| offset(8), | ||
| flip({ | ||
| boundary: props.boundary ?? 'clippingAncestors', | ||
| fallbackStrategy: 'initialPlacement', | ||
| }), | ||
| shift({ | ||
| padding: 8, | ||
| boundary: props.boundary ?? 'clippingAncestors', | ||
| }), | ||
| ], | ||
| }); | ||
| setPos({ x, y }); | ||
| }; | ||
|
|
||
| const cleanupAutoUpdate = autoUpdate(props.anchor, ref, update); | ||
|
|
||
| const handlePointerDown = (e: PointerEvent) => { | ||
| if ( | ||
| !ref.contains(e.target as Node) && | ||
| !props.anchor.contains(e.target as Node) | ||
| ) { | ||
| props.onClose(); | ||
| } | ||
| }; | ||
|
|
||
| const handleKeyDown = (e: KeyboardEvent) => { | ||
| if (e.key === 'Escape') { | ||
| e.stopPropagation(); | ||
| props.onClose(); | ||
| } | ||
| }; | ||
|
|
||
| document.addEventListener('pointerdown', handlePointerDown); | ||
| document.addEventListener('keydown', handleKeyDown); | ||
|
|
||
| onCleanup(() => { | ||
| cleanupAutoUpdate(); | ||
| document.removeEventListener('pointerdown', handlePointerDown); | ||
| document.removeEventListener('keydown', handleKeyDown); | ||
| }); | ||
| }); | ||
|
|
||
| return ( | ||
| <div | ||
| ref={ref} | ||
| style={{ position: 'fixed', left: `${pos().x}px`, top: `${pos().y}px` }} | ||
| class="bg-menu w-fit p-1 border border-edge-muted rounded-xs shadow z-highlight-menu" | ||
| > | ||
| {props.children} | ||
| </div> | ||
| ); | ||
| } | ||
|
|
||
| // --- Context --- | ||
|
|
||
| type SimpleDropdownContextValue = { | ||
| open: Accessor<boolean>; | ||
| onOpenChange: (open: boolean) => void; | ||
| boundary: Accessor<HTMLElement | null>; | ||
| anchor: Accessor<HTMLElement | undefined>; | ||
| setAnchor: (el: HTMLElement) => void; | ||
| }; | ||
|
|
||
| const SimpleDropdownContext = createContext<SimpleDropdownContextValue>(); | ||
|
|
||
| function useSimpleDropdownContext() { | ||
| const ctx = useContext(SimpleDropdownContext); | ||
| if (!ctx) | ||
| throw new Error( | ||
| 'SimpleDropdown sub-components must be used inside <SimpleDropdown>' | ||
| ); | ||
| return ctx; | ||
| } | ||
|
|
||
| // --- Item --- | ||
|
|
||
| export type DropdownItemProps = { | ||
| text: string | JSX.Element; | ||
| icon?: Component<JSX.SvgSVGAttributes<SVGSVGElement>>; | ||
| onClick?: () => void; | ||
| disabled?: boolean; | ||
| class?: string; | ||
| }; | ||
|
|
||
| const ITEM_BASE_CLASS = `flex flex-row w-full gap-1.5 tracking-tight ${isMobile() ? 'py-2 px-1 text-base' : 'py-1 pl-2 pr-2 text-sm'} font-medium justify-between items-center focus-bracket rounded-xs`; | ||
|
|
||
| function ItemInner(props: Pick<DropdownItemProps, 'icon' | 'text'>) { | ||
| return ( | ||
| <> | ||
| <Show when={props.icon}> | ||
| <Dynamic | ||
| component={props.icon} | ||
| class={cn('shrink-0', isMobile() ? 'w-5 h-5' : 'w-4 h-4')} | ||
| /> | ||
| </Show> | ||
| <Show when={props.text}> | ||
| <div class="flex-1 truncate">{props.text}</div> | ||
| </Show> | ||
| </> | ||
| ); | ||
| } | ||
|
|
||
| function TouchItem(props: DropdownItemProps) { | ||
| return ( | ||
| <div | ||
| onClick={props.onClick} | ||
| class={cn( | ||
| ITEM_BASE_CLASS, | ||
| props.disabled | ||
| ? 'opacity-50 cursor-not-allowed' | ||
| : 'hover:bg-hover hover-transition-bg', | ||
| props.class | ||
| )} | ||
| > | ||
| <ItemInner icon={props.icon} text={props.text} /> | ||
| </div> | ||
| ); | ||
| } | ||
|
|
||
| function KobalteItem(props: DropdownItemProps) { | ||
| return ( | ||
| <DropdownMenu.Item | ||
| onSelect={props.onClick} | ||
| disabled={props.disabled} | ||
| class={cn( | ||
| ITEM_BASE_CLASS, | ||
| props.disabled | ||
| ? 'opacity-50 cursor-not-allowed' | ||
| : 'hover:bg-hover hover-transition-bg', | ||
| props.class | ||
| )} | ||
| > | ||
| <ItemInner icon={props.icon} text={props.text} /> | ||
| </DropdownMenu.Item> | ||
| ); | ||
| } | ||
|
|
||
| const DropdownItem = isTouchDevice() ? TouchItem : KobalteItem; | ||
|
peterchinman marked this conversation as resolved.
|
||
|
|
||
| // --- Sub-components --- | ||
|
|
||
| function SimpleDropdownTrigger(props: { | ||
| as?: ValidComponent; | ||
| onClick?: (e: MouseEvent) => void; | ||
| children?: JSX.Element; | ||
| [key: string]: unknown; | ||
| }) { | ||
| const ctx = useSimpleDropdownContext(); | ||
| const [local, others] = splitProps(props, ['as', 'onClick']); | ||
| return ( | ||
| <Dynamic | ||
| component={(local.as ?? 'button') as ValidComponent} | ||
| ref={(el: HTMLElement) => ctx.setAnchor(el)} | ||
| onClick={(e: MouseEvent) => { | ||
| local.onClick?.(e); | ||
| ctx.onOpenChange(!ctx.open()); | ||
| }} | ||
| {...(others as Record<string, unknown>)} | ||
| /> | ||
| ); | ||
| } | ||
|
|
||
| // No-op: portaling is handled internally by Content. | ||
| function SimpleDropdownPortal(props: { children: JSX.Element }) { | ||
| return <>{props.children}</>; | ||
| } | ||
|
|
||
| function SimpleDropdownContent(props: { | ||
| class?: string; | ||
| children: JSX.Element; | ||
| }) { | ||
| const ctx = useSimpleDropdownContext(); | ||
| return ( | ||
| <Show when={ctx.open() && ctx.anchor()}> | ||
| <Portal> | ||
| <FloatingContent | ||
| anchor={ctx.anchor()!} | ||
| boundary={ctx.boundary()} | ||
| onClose={() => ctx.onOpenChange(false)} | ||
| > | ||
| {props.children} | ||
| </FloatingContent> | ||
| </Portal> | ||
| </Show> | ||
| ); | ||
| } | ||
|
|
||
| // --- Root --- | ||
|
|
||
| function SimpleDropdownRoot(props: { | ||
| open: boolean; | ||
| onOpenChange: (open: boolean) => void; | ||
| boundary?: Accessor<HTMLElement | null>; | ||
| children: JSX.Element; | ||
| }) { | ||
| const [anchor, setAnchor] = createSignal<HTMLElement>(); | ||
| return ( | ||
| <SimpleDropdownContext.Provider | ||
| value={{ | ||
| open: () => props.open, | ||
| onOpenChange: props.onOpenChange, | ||
| boundary: props.boundary ?? (() => null), | ||
| anchor, | ||
| setAnchor, | ||
| }} | ||
| > | ||
| {props.children} | ||
| </SimpleDropdownContext.Provider> | ||
| ); | ||
| } | ||
|
|
||
| export const SimpleDropdown = Object.assign(SimpleDropdownRoot, { | ||
| Trigger: SimpleDropdownTrigger, | ||
| Portal: SimpleDropdownPortal, | ||
| Content: SimpleDropdownContent, | ||
| Item: DropdownItem, | ||
| }); | ||
|
|
||
| export type DropdownMenuLike = { | ||
| (props: { | ||
| open: boolean; | ||
| onOpenChange: (v: boolean) => void; | ||
| boundary?: unknown; | ||
| children: JSX.Element; | ||
| }): JSX.Element; | ||
| Trigger: Component<any>; | ||
| Portal: Component<any>; | ||
| Content: Component<any>; | ||
| Item: Component<any>; | ||
| }; | ||
|
|
||
| // On desktop: use Kobalte's root/trigger/portal/content (for keyboard nav and | ||
| // focus management) but replace Item with the wrapped KobalteItem so that | ||
| // callers can use the text/icon/onClick interface. | ||
| const DesktopDropdown = Object.assign( | ||
| (props: any) => <DropdownMenu {...props} />, | ||
| { | ||
| Trigger: DropdownMenu.Trigger, | ||
| Portal: DropdownMenu.Portal, | ||
| Content: DropdownMenu.Content, | ||
| Item: KobalteItem, | ||
| } | ||
| ); | ||
|
|
||
| export const ResponsiveDropdown: DropdownMenuLike = isTouchDevice() | ||
| ? (SimpleDropdown as unknown as DropdownMenuLike) | ||
| : (DesktopDropdown as unknown as DropdownMenuLike); | ||
|
peterchinman marked this conversation as resolved.
|
||
This file contains hidden or 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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.