Skip to content

Commit

Permalink
refactor: offsetless middlewares
Browse files Browse the repository at this point in the history
  • Loading branch information
mary-ext committed Dec 10, 2023
1 parent 415a123 commit ea993a4
Show file tree
Hide file tree
Showing 9 changed files with 39 additions and 63 deletions.
24 changes: 22 additions & 2 deletions app/com/components/Flyout.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { type JSX, Show, createSignal, untrack } from 'solid-js';

import { type Middleware, autoUpdate, flip, shift } from '@floating-ui/dom';
import { type Middleware, autoUpdate, flip, shift, size } from '@floating-ui/dom';
import { type Placement, getSide } from '@floating-ui/utils';
import { useFloating } from 'solid-floating-ui';

Expand Down Expand Up @@ -43,6 +43,26 @@ export const offset: Middleware = {
},
};

export const offsetlessMiddlewares = [
shift({
padding: 16,
}),
flip({
padding: 16,
}),
size({
padding: 16,
apply({ availableWidth, availableHeight, elements }) {
Object.assign(elements.floating.style, {
maxWidth: `${availableWidth}px`,
maxHeight: `${availableHeight}px`,
});
},
}),
];

const defaultMiddlewares: Middleware[] = [offset, ...offsetlessMiddlewares];

export const Flyout = (props: FlyoutProps) => {
const [isOpen, setIsOpen] = createSignal(false);

Expand Down Expand Up @@ -76,7 +96,7 @@ export const Flyout = (props: FlyoutProps) => {
const position = useFloating(() => anchor, floating, {
placement: props.placement ?? 'bottom-end',
strategy: 'absolute',
middleware: props.middleware ?? [shift({ padding: 16 }), offset, flip()],
middleware: props.middleware ?? defaultMiddlewares,
whileElementsMounted: autoUpdate,
});

Expand Down
21 changes: 2 additions & 19 deletions app/com/components/embeds/images/ImageAltAction.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import type { JSX } from 'solid-js';

import { flip, shift, size } from '@floating-ui/dom';

import { Button } from '../../../primitives/button.ts';

import { Flyout } from '../../Flyout.tsx';
import { Flyout, offsetlessMiddlewares } from '../../Flyout.tsx';

export interface ImageAltActionProps {
alt: string;
Expand All @@ -14,22 +12,7 @@ export interface ImageAltActionProps {
const ImageAltAction = (props: ImageAltActionProps) => {
if (import.meta.env.VITE_MODE === 'desktop') {
return (
<Flyout
button={props.children}
placement="bottom"
middleware={[
flip(),
shift({ padding: 16 }),
size({
padding: 16,
apply({ availableHeight, elements }) {
Object.assign(elements.floating.style, {
maxHeight: `${availableHeight}px`,
});
},
}),
]}
>
<Flyout button={props.children} placement="bottom" middleware={offsetlessMiddlewares}>
{({ close, menuProps }) => (
<div
{...menuProps}
Expand Down
6 changes: 3 additions & 3 deletions app/com/components/inputs/AddPhotoButton.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { type JSX, createSignal } from 'solid-js';

import { type Middleware, flip } from '@floating-ui/dom';
import { type Middleware } from '@floating-ui/dom';
import { getSide } from '@floating-ui/utils';

import { compressProfileImage } from '~/utils/image.ts';
Expand All @@ -9,7 +9,7 @@ import { openModal } from '~/com/globals/modals.tsx';

import { MenuItem, MenuRoot } from '../../primitives/menu.ts';

import { Flyout } from '../Flyout.tsx';
import { Flyout, offsetlessMiddlewares } from '../Flyout.tsx';
import CircularProgress from '../CircularProgress.tsx';

import AddPhotoAlternateIcon from '../../icons/baseline-add-photo-alternate.tsx';
Expand Down Expand Up @@ -119,7 +119,7 @@ const AddPhotoButton = (props: AddPhotoButtonProps) => {

if (shouldDisplayFlyout) {
return (
<Flyout button={button} placement="bottom" middleware={[flip(), buttonOffset]}>
<Flyout button={button} placement="bottom" middleware={[buttonOffset, ...offsetlessMiddlewares]}>
{({ close, menuProps }) => (
<div {...menuProps} class={/* @once */ MenuRoot()}>
<button
Expand Down
10 changes: 2 additions & 8 deletions app/com/components/views/profiles/ProfileHandleAction.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
import { type JSX } from 'solid-js';

import { flip, shift } from '@floating-ui/dom';

import type { SignalizedProfile } from '~/api/stores/profiles.ts';

import { MenuItem, MenuItemIcon, MenuRoot } from '../../../primitives/menu.ts';

import { Flyout } from '../../Flyout.tsx';
import { Flyout, offsetlessMiddlewares } from '../../Flyout.tsx';

import ContentCopyIcon from '~/com/icons/baseline-content-copy.tsx';

Expand All @@ -21,11 +19,7 @@ const ProfileHandleAction = (props: ProfileHandleActionProps) => {

if (import.meta.env.VITE_MODE === 'desktop') {
return (
<Flyout
button={props.children}
placement="bottom-start"
middleware={[shift({ padding: 16 }), flip()]}
>
<Flyout button={props.children} placement="bottom-start" middleware={offsetlessMiddlewares}>
{({ close, menuProps }) => (
<div {...menuProps} class={/* @once */ MenuRoot()}>
<button
Expand Down
2 changes: 1 addition & 1 deletion app/com/primitives/menu.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Interactive } from './interactive.ts';

export const MenuRoot = () => {
return `flex max-w-sm flex-col overflow-hidden rounded-lg bg-background shadow-menu`;
return `flex max-w-sm flex-col overflow-hidden overflow-y-auto rounded-lg bg-background shadow-menu`;
};

export interface MenuItemProps {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import type { JSX } from 'solid-js';

import { flip, shift } from '@floating-ui/dom';

import { MenuItem, MenuRoot } from '~/com/primitives/menu.ts';

import { Flyout } from '~/com/components/Flyout.tsx';
import { Flyout, offsetlessMiddlewares } from '~/com/components/Flyout.tsx';

import CheckIcon from '~/com/icons/baseline-check.tsx';

Expand All @@ -16,7 +14,7 @@ export interface ContentWarningActionProps {

const ContentWarningAction = (props: ContentWarningActionProps) => {
return (
<Flyout button={props.children} placement="bottom" middleware={[shift({ padding: 16 }), flip()]}>
<Flyout button={props.children} placement="bottom" middleware={offsetlessMiddlewares}>
{({ close, menuProps }) => {
const renderItem = (value: string | undefined, label: string, description: string) => {
return (
Expand Down
21 changes: 2 additions & 19 deletions app/desktop/components/composer/actions/PostLanguageAction.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import type { JSX } from 'solid-js';

import { flip, shift, size } from '@floating-ui/dom';

import { systemLanguages } from '~/api/globals/platform.ts';

import { getNativeLanguageName, languageNames } from '~/utils/intl/display-names.ts';
Expand All @@ -12,7 +10,7 @@ import { preferences } from '~/desktop/globals/settings.ts';

import { MenuItem, MenuRoot } from '~/com/primitives/menu.ts';

import { Flyout } from '~/com/components/Flyout.tsx';
import { Flyout, offsetlessMiddlewares } from '~/com/components/Flyout.tsx';

import CheckIcon from '~/com/icons/baseline-check.tsx';

Expand Down Expand Up @@ -45,22 +43,7 @@ const getLanguageCodes = () => {

const PostLanguageAction = (props: PostLanguageActionProps) => {
return (
<Flyout
button={props.children}
placement="bottom"
middleware={[
shift({ padding: 16 }),
flip(),
size({
padding: 16,
apply({ availableHeight, elements }) {
Object.assign(elements.floating.style, {
maxHeight: `${availableHeight}px`,
});
},
}),
]}
>
<Flyout button={props.children} placement="bottom" middleware={offsetlessMiddlewares}>
{({ close, menuProps }) => {
const languages = getLanguageCodes();

Expand Down
6 changes: 2 additions & 4 deletions app/desktop/components/flyouts/SwitchAccountAction.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
import type { JSX } from 'solid-js';

import { flip, shift } from '@floating-ui/dom';

import type { DID } from '~/api/atp-schema.ts';
import { multiagent } from '~/api/globals/agent.ts';

import { MenuItem, MenuRoot } from '~/com/primitives/menu.ts';

import { Flyout } from '~/com/components/Flyout.tsx';
import { Flyout, offsetlessMiddlewares } from '~/com/components/Flyout.tsx';

import DefaultUserAvatar from '~/com/assets/default-user-avatar.svg?url';
import CheckIcon from '~/com/icons/baseline-check.tsx';
Expand All @@ -20,7 +18,7 @@ export interface SwitchAccountActionProps {

const SwitchAccountAction = (props: SwitchAccountActionProps) => {
return (
<Flyout button={props.children} middleware={[shift({ padding: 16 }), flip()]} placement="bottom">
<Flyout button={props.children} middleware={offsetlessMiddlewares} placement="bottom">
{({ close, menuProps }) => (
<div {...menuProps} class={/* @once */ MenuRoot()}>
{multiagent.accounts.map((account) => (
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { type JSX, batch } from 'solid-js';

import { type Middleware, flip } from '@floating-ui/dom';
import { type Middleware } from '@floating-ui/dom';

import { renderLabelGroupName, renderLabelName } from '~/api/display.ts';
import { getSide } from '@floating-ui/utils';
Expand All @@ -12,7 +12,7 @@ import { assert } from '~/utils/misc.ts';

import { preferences } from '~/desktop/globals/settings.ts';

import { Flyout } from '~/com/components/Flyout.tsx';
import { Flyout, offsetlessMiddlewares } from '~/com/components/Flyout.tsx';

import { IconButton } from '~/com/primitives/icon-button.ts';
import { Interactive } from '~/com/primitives/interactive.ts';
Expand Down Expand Up @@ -359,7 +359,7 @@ const offsetMiddleware: Middleware = {

const LabelConfigFlyout = (props: LabelConfigFlyoutProps) => {
return (
<Flyout button={props.children} middleware={[offsetMiddleware, flip()]}>
<Flyout button={props.children} middleware={[offsetMiddleware, ...offsetlessMiddlewares]}>
{({ close, menuProps }) => {
const renderItem = (value: number | undefined, title: string, subtitle?: string) => {
return (
Expand Down

0 comments on commit ea993a4

Please sign in to comment.