refactor: use ariakit for super note popovers

This commit is contained in:
Aman Harwara
2023-12-14 16:03:18 +05:30
parent caab23d8d1
commit 3c5c6723ac
4 changed files with 90 additions and 94 deletions

View File

@@ -19,9 +19,7 @@ export function BlockPickerMenuItem({
<li <li
key={option.key} key={option.key}
tabIndex={-1} tabIndex={-1}
className={`border-bottom gap-3 border-[0.5px] border-border ${PopoverItemClassNames} ${ className={`gap-3 ${PopoverItemClassNames} ${isSelected ? PopoverItemSelectedClassNames : ''}`}
isSelected ? PopoverItemSelectedClassNames : ''
}`}
ref={option.setRefElement} ref={option.setRefElement}
role="option" role="option"
aria-selected={isSelected} aria-selected={isSelected}

View File

@@ -8,9 +8,7 @@ import { BlockPickerOption } from './BlockPickerOption'
import { BlockPickerMenuItem } from './BlockPickerMenuItem' import { BlockPickerMenuItem } from './BlockPickerMenuItem'
import { GetDynamicPasswordBlocks, GetPasswordBlockOption } from '../Blocks/Password' import { GetDynamicPasswordBlocks, GetPasswordBlockOption } from '../Blocks/Password'
import { GetDynamicTableBlocks, GetTableBlockOption } from '../Blocks/Table' import { GetDynamicTableBlocks, GetTableBlockOption } from '../Blocks/Table'
import Popover from '@/Components/Popover/Popover'
import { GetDatetimeBlockOptions } from '../Blocks/DateTime' import { GetDatetimeBlockOptions } from '../Blocks/DateTime'
import { isMobileScreen } from '@/Utils'
import { useApplication } from '@/Components/ApplicationProvider' import { useApplication } from '@/Components/ApplicationProvider'
import { GetRemoteImageBlockOption } from '../Blocks/RemoteImage' import { GetRemoteImageBlockOption } from '../Blocks/RemoteImage'
import { InsertRemoteImageDialog } from '../RemoteImagePlugin/RemoteImagePlugin' import { InsertRemoteImageDialog } from '../RemoteImagePlugin/RemoteImagePlugin'
@@ -29,6 +27,8 @@ import { GetQuoteBlockOption } from '../Blocks/Quote'
import { GetDividerBlockOption } from '../Blocks/Divider' import { GetDividerBlockOption } from '../Blocks/Divider'
import { GetCollapsibleBlockOption } from '../Blocks/Collapsible' import { GetCollapsibleBlockOption } from '../Blocks/Collapsible'
import { GetEmbedsBlockOptions } from '../Blocks/Embeds' import { GetEmbedsBlockOptions } from '../Blocks/Embeds'
import { Popover, PopoverProvider } from '@ariakit/react'
import { PopoverClassNames } from '../ClassNames'
export default function BlockPickerMenuPlugin(): JSX.Element { export default function BlockPickerMenuPlugin(): JSX.Element {
const [editor] = useLexicalComposerContext() const [editor] = useLexicalComposerContext()
@@ -111,6 +111,7 @@ export default function BlockPickerMenuPlugin(): JSX.Element {
return ( return (
<> <>
{modal} {modal}
<PopoverProvider>
<LexicalTypeaheadMenuPlugin<BlockPickerOption> <LexicalTypeaheadMenuPlugin<BlockPickerOption>
onQueryChange={setQueryString} onQueryChange={setQueryString}
onSelectOption={onSelectOption} onSelectOption={onSelectOption}
@@ -120,18 +121,16 @@ export default function BlockPickerMenuPlugin(): JSX.Element {
if (!anchorElementRef.current || !options.length) { if (!anchorElementRef.current || !options.length) {
return null return null
} }
return ( return (
<Popover <Popover
title="Block picker" open
align="start" getAnchorRect={() => anchorElementRef.current!.getBoundingClientRect()}
anchorElement={anchorElementRef.current} autoFocus={false}
open={true} autoFocusOnShow={false}
disableMobileFullscreenTakeover={true} className={PopoverClassNames}
side={isMobileScreen() ? 'top' : 'bottom'} fitViewport
maxHeight={(mh) => mh / 2}
> >
<ul> <ul className="divide-y divide-border">
{options.map((option, i: number) => ( {options.map((option, i: number) => (
<BlockPickerMenuItem <BlockPickerMenuItem
index={i} index={i}
@@ -152,6 +151,7 @@ export default function BlockPickerMenuPlugin(): JSX.Element {
) )
}} }}
/> />
</PopoverProvider>
</> </>
) )
} }

View File

@@ -1,9 +1,7 @@
import { classNames } from '@standardnotes/utils' import { classNames } from '@standardnotes/utils'
export const PopoverClassNames = classNames( export const PopoverClassNames =
'z-dropdown-menu w-full', 'max-h-[min(var(--popover-available-height,_50vh),_50vh)] w-[--popover-available-width] overflow-y-auto rounded border border-[--popover-border-color] bg-default shadow-main [backdrop-filter:var(--popover-backdrop-filter)] md:max-w-xs md:bg-[--popover-background-color]'
'cursor-auto flex-col overflow-y-auto rounded bg-default h-auto',
)
export const PopoverItemClassNames = classNames( export const PopoverItemClassNames = classNames(
'flex w-full items-center text-base overflow-hidden py-2 px-3 hover:bg-contrast hover:text-foreground', 'flex w-full items-center text-base overflow-hidden py-2 px-3 hover:bg-contrast hover:text-foreground',

View File

@@ -7,11 +7,11 @@ import { ItemOption } from './ItemOption'
import { useApplication } from '@/Components/ApplicationProvider' import { useApplication } from '@/Components/ApplicationProvider'
import { ContentType, SNNote } from '@standardnotes/snjs' import { ContentType, SNNote } from '@standardnotes/snjs'
import { getLinkingSearchResults } from '@/Utils/Items/Search/getSearchResults' import { getLinkingSearchResults } from '@/Utils/Items/Search/getSearchResults'
import Popover from '@/Components/Popover/Popover'
import { INSERT_BUBBLE_COMMAND, INSERT_FILE_COMMAND } from '../Commands' import { INSERT_BUBBLE_COMMAND, INSERT_FILE_COMMAND } from '../Commands'
import { useLinkingController } from '../../../../Controllers/LinkingControllerProvider' import { useLinkingController } from '../../../../Controllers/LinkingControllerProvider'
import { isMobileScreen } from '@/Utils'
import { useTypeaheadAllowingSpacesAndPunctuation } from './useTypeaheadAllowingSpacesAndPunctuation' import { useTypeaheadAllowingSpacesAndPunctuation } from './useTypeaheadAllowingSpacesAndPunctuation'
import { Popover, PopoverProvider } from '@ariakit/react'
import { PopoverClassNames } from '../ClassNames'
type Props = { type Props = {
currentNote: SNNote currentNote: SNNote
@@ -83,6 +83,7 @@ export const ItemSelectionPlugin: FunctionComponent<Props> = ({ currentNote }) =
}, [application, editor, currentNote, queryString, linkingController]) }, [application, editor, currentNote, queryString, linkingController])
return ( return (
<PopoverProvider>
<LexicalTypeaheadMenuPlugin<ItemOption> <LexicalTypeaheadMenuPlugin<ItemOption>
onQueryChange={setQueryString} onQueryChange={setQueryString}
onSelectOption={onSelectOption} onSelectOption={onSelectOption}
@@ -92,18 +93,16 @@ export const ItemSelectionPlugin: FunctionComponent<Props> = ({ currentNote }) =
if (!anchorElementRef.current || !options.length) { if (!anchorElementRef.current || !options.length) {
return null return null
} }
return ( return (
<Popover <Popover
title="Select item" open
align="start" getAnchorRect={() => anchorElementRef.current!.getBoundingClientRect()}
anchorElement={anchorElementRef} autoFocus={false}
open={true} autoFocusOnShow={false}
disableMobileFullscreenTakeover={true} className={PopoverClassNames}
side={isMobileScreen() ? 'top' : 'bottom'} fitViewport
maxHeight={(mh) => mh / 2}
> >
<ul> <ul className="divide-y divide-border">
{options.map((option, i: number) => ( {options.map((option, i: number) => (
<ItemSelectionItemComponent <ItemSelectionItemComponent
searchQuery={queryString || ''} searchQuery={queryString || ''}
@@ -125,5 +124,6 @@ export const ItemSelectionPlugin: FunctionComponent<Props> = ({ currentNote }) =
) )
}} }}
/> />
</PopoverProvider>
) )
} }