Skip to content

Support rtl in floating.tsx #1109

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
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/warm-groups-reply.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@qwik-ui/headless': patch
---

Support rtl for elements using floating ui
26 changes: 23 additions & 3 deletions apps/website/src/routes/docs/headless/popover/index.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,31 @@ title: Qwik UI | Popover
import { statusByComponent } from '~/_state/component-statuses';
import Styles from './examples/styles';

import { ShowcaseHero, ShowcaseBasic, ShowcaseInspect, ShowcaseAuto, ShowcaseManual, ShowcaseProgrammatic, ShowcaseToggleEvent, ShowcaseFloating, ShowcasePlacement, ShowcaseCorners, ShowcaseFlip, ShowcaseGutter, ShowcaseStyling, ShowcaseAnimation, ShowcaseTransition } from './showcase-components';
import { CodeSnippetFloatingCss, CodeSnippetPopoverCss, CodeSnippetBuildingBlocks } from './code-snippets';
import {
ShowcaseHero,
ShowcaseBasic,
ShowcaseInspect,
ShowcaseAuto,
ShowcaseManual,
ShowcaseProgrammatic,
ShowcaseToggleEvent,
ShowcaseFloating,
ShowcasePlacement,
ShowcaseCorners,
ShowcaseFlip,
ShowcaseGutter,
ShowcaseStyling,
ShowcaseAnimation,
ShowcaseTransition,
} from './showcase-components';
import {
CodeSnippetFloatingCss,
CodeSnippetPopoverCss,
CodeSnippetBuildingBlocks,
} from './code-snippets';

<StatusBanner status={statusByComponent.headless.Popover} />


# Popover

A popup that goes above other content on the page. You can still interact with the rest of the page while the popover is open.
Expand All @@ -24,6 +43,7 @@ A popup that goes above other content on the page. You can still interact with t
'Polyfill for unsupported browsers',
'Executes code on interaction',
'Float and position the popover',
'Supports RTL',
]}
/>

Expand Down
19 changes: 15 additions & 4 deletions apps/website/src/routes/docs/headless/tooltip/index.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,19 @@ title: Qwik UI | Tooltip

import { statusByComponent } from '~/_state/component-statuses';

import { ShowcaseHero, ShowcaseFloating, ShowcasePlacement, ShowcaseFlip, ShowcaseGutter, ShowcaseStyling, ShowcaseAnimation, ShowcaseTransition, ShowcaseOnChange, ShowcaseBasic, ShowcaseComplex } from './showcase-components';
import {
ShowcaseHero,
ShowcaseFloating,
ShowcasePlacement,
ShowcaseFlip,
ShowcaseGutter,
ShowcaseStyling,
ShowcaseAnimation,
ShowcaseTransition,
ShowcaseOnChange,
ShowcaseBasic,
ShowcaseComplex,
} from './showcase-components';

<StatusBanner status={statusByComponent.headless.Tooltip} />

Expand All @@ -25,6 +37,7 @@ A text label that appears when a user hovers, focuses, or touches an element.
'Accessibility with ARIA roles and keyboard interactions',
'Flipping to avoid overflow',
'Automatic placement adjustment',
'Supports RTL',
]}
/>

Expand All @@ -42,9 +55,7 @@ export default component$(() => {
<Tooltip.Trigger>
<button>Hover or Focus me</button>
</Tooltip.Trigger>
<Tooltip.Panel aria-label="Tooltip content">
Tooltip content here
</Tooltip.Panel>
<Tooltip.Panel aria-label="Tooltip content">Tooltip content here</Tooltip.Panel>
</Tooltip.Root>
);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export const HDropdownPopover = component$<PropsOf<typeof HPopoverRoot>>((props)
const { showPopover, hidePopover } = usePopover(context.localId);
const initialLoadSig = useSignal<boolean>(true);

const { floating, flip, hover, gutter, ...rest } = props;
const { floating, flip, hover, gutter, shift, ...rest } = props;

useTask$(async ({ track }) => {
track(() => context.isOpenSig.value);
Expand Down Expand Up @@ -103,6 +103,7 @@ export const HDropdownPopover = component$<PropsOf<typeof HPopoverRoot>>((props)
manual
id={context.localId}
style={{ display: 'contents' }}
shift={shift}
>
<HPopoverPanel
id={menuId}
Expand Down
24 changes: 19 additions & 5 deletions packages/kit-headless/src/components/popover/floating.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,25 @@ export const FloatingPopover = component$((props: PropsOf<'div'>) => {
}).then(async (resolvedData) => {
const { x, y } = resolvedData;

Object.assign(popover.style, {
left: `${x}px`,
top: `${y}px`,
transform: context.transform,
});
const isRTL = document.documentElement.dir === 'rtl';

if (isRTL) {
const documentWidth = document.body.getBoundingClientRect().width;
const popoverWidth = popover.getBoundingClientRect().width;
const boundaryX = Math.max(x, 0);

Object.assign(popover.style, {
right: `${documentWidth - boundaryX - popoverWidth}px`,
top: `${y}px`,
transform: context.transform,
});
} else {
Object.assign(popover.style, {
left: `${x}px`,
top: `${y}px`,
transform: context.transform,
});
}

if (resolvedData.middlewareData.arrow && context.arrowRef?.value) {
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didnt yet add rtl support for the arrow but it doesnt seem to be working as expected with arrows at the given moment or at least didnt see a working example, so I ignored it atm.

const { x, y } = resolvedData.middlewareData.arrow;
Expand Down