Skip to content

armgit / dify

Public Imported from GitHub
0

Build Agentic workflows, RAG pipelines, with rich AI model and tool support on one collaborative workspace. Deploy on cloud, VPC, or self-hosted, so teams move from prototype to production without rebuilding the stack.

fix(dify-ui): complete RTL styling for directional controls (#43808)

yyh0afbe55884d6
diff --git a/packages/dify-ui/src/autocomplete/index.tsx b/packages/dify-ui/src/autocomplete/index.tsx
index 1f35fe799b4..cc69d4b0fad 100644
--- a/packages/dify-ui/src/autocomplete/index.tsx
+++ b/packages/dify-ui/src/autocomplete/index.tsx
@@ -390,7 +390,12 @@ function AutocompleteItemIndicator({
}: AutocompleteItemIndicatorProps) {
return (
<span className={cn(floatingItemIndicatorClassName, className)} {...props}>
- {children ?? <span className="i-ri-arrow-right-line size-4" aria-hidden="true" />}
+ {children ?? (
+ <span
+ className="i-ri-arrow-right-line size-4 [&:dir(rtl)]:-scale-x-100"
+ aria-hidden="true"
+ />
+ )}
</span>
)
}
diff --git a/packages/dify-ui/src/context-menu/index.tsx b/packages/dify-ui/src/context-menu/index.tsx
index 8aa9ead7237..cd29f014192 100644
--- a/packages/dify-ui/src/context-menu/index.tsx
+++ b/packages/dify-ui/src/context-menu/index.tsx
@@ -209,7 +209,7 @@ function ContextMenuSubTrigger({
{children}
<span
aria-hidden
- className="ms-auto i-ri-arrow-right-s-line size-4 shrink-0 text-text-tertiary"
+ className="ms-auto i-ri-arrow-right-s-line size-4 shrink-0 text-text-tertiary [&:dir(rtl)]:-scale-x-100"
/>
</BaseContextMenu.SubmenuTrigger>
)
diff --git a/packages/dify-ui/src/dropdown-menu/index.stories.tsx b/packages/dify-ui/src/dropdown-menu/index.stories.tsx
index e42555366cd..693114b97c6 100644
--- a/packages/dify-ui/src/dropdown-menu/index.stories.tsx
+++ b/packages/dify-ui/src/dropdown-menu/index.stories.tsx
@@ -26,6 +26,7 @@ import {
DropdownMenuSubTrigger,
DropdownMenuTrigger,
} from '.'
+import { DirectionProvider } from '../direction-provider'
function TriggerButton({ label = 'Open Menu' }: { label?: string }) {
return (
@@ -148,6 +149,78 @@ export const WithSubmenu: Story = {
),
}
+export const RTL: Story = {
+ parameters: {
+ docs: {
+ story: { autoplay: false },
+ description: {
+ story:
+ 'DirectionProvider controls keyboard navigation and placement. Set dir on the trigger wrapper and each portaled content when using a local RTL region. Submenus open with ArrowLeft and close with ArrowRight.',
+ },
+ },
+ },
+ render: () => (
+ <DirectionProvider direction="rtl">
+ <div
+ lang="ar"
+ dir="rtl"
+ className="flex min-h-64 w-[min(36rem,calc(100vw-2rem))] items-start"
+ >
+ <DropdownMenu>
+ <TriggerButton label="الإجراءات" />
+ <DropdownMenuContent lang="ar" dir="rtl">
+ <DropdownMenuSub>
+ <DropdownMenuSubTrigger>
+ <span>مشاركة</span>
+ </DropdownMenuSubTrigger>
+ <DropdownMenuSubContent lang="ar" dir="rtl">
+ <DropdownMenuItem>البريد الإلكتروني</DropdownMenuItem>
+ <DropdownMenuItem>نسخ الرابط</DropdownMenuItem>
+ </DropdownMenuSubContent>
+ </DropdownMenuSub>
+ <DropdownMenuItem>تنزيل</DropdownMenuItem>
+ </DropdownMenuContent>
+ </DropdownMenu>
+ </div>
+ </DirectionProvider>
+ ),
+ play: async ({ canvasElement }) => {
+ const canvas = within(canvasElement)
+ const body = within(canvasElement.ownerDocument.body)
+ const trigger = canvas.getByRole('button', { name: 'الإجراءات' })
+ trigger.focus()
+ await userEvent.keyboard('{ArrowDown}')
+ const share = await body.findByRole('menuitem', { name: 'مشاركة' })
+ await waitFor(() => expect(share).toHaveFocus())
+
+ const arrow = share.querySelector('[aria-hidden]')!
+ await expect(getComputedStyle(arrow).scale).toBe('-1 1')
+ await expect(arrow.getBoundingClientRect().right).toBeLessThanOrEqual(
+ within(share).getByText('مشاركة').getBoundingClientRect().left,
+ )
+
+ await userEvent.keyboard('{ArrowLeft}')
+ const submenu = await body.findByRole('menu', { name: 'مشاركة' })
+ await waitFor(() =>
+ expect(body.getByRole('menuitem', { name: 'البريد الإلكتروني' })).toHaveFocus(),
+ )
+ await waitFor(() => {
+ expect(submenu.getBoundingClientRect().right).toBeLessThanOrEqual(
+ share.getBoundingClientRect().left,
+ )
+ })
+
+ await userEvent.keyboard('{ArrowRight}')
+ await waitFor(() => expect(share).toHaveFocus())
+ await waitFor(() =>
+ expect(body.queryByRole('menu', { name: 'مشاركة' })).not.toBeInTheDocument(),
+ )
+ await userEvent.keyboard('{Escape}')
+ await waitFor(() => expect(trigger).toHaveFocus())
+ await expect(trigger).toHaveAttribute('aria-expanded', 'false')
+ },
+}
+
type Density = 'compact' | 'comfortable' | 'spacious'
function WithRadioItemsDemo() {
diff --git a/packages/dify-ui/src/dropdown-menu/index.tsx b/packages/dify-ui/src/dropdown-menu/index.tsx
index 9621266402d..9438c24ea42 100644
--- a/packages/dify-ui/src/dropdown-menu/index.tsx
+++ b/packages/dify-ui/src/dropdown-menu/index.tsx
@@ -329,7 +329,7 @@ function DropdownMenuSubTrigger({
{children}
<span
aria-hidden
- className="ms-auto i-ri-arrow-right-s-line size-4 shrink-0 text-text-tertiary"
+ className="ms-auto i-ri-arrow-right-s-line size-4 shrink-0 text-text-tertiary [&:dir(rtl)]:-scale-x-100"
/>
</Menu.SubmenuTrigger>
)
diff --git a/packages/dify-ui/src/number-field/index.tsx b/packages/dify-ui/src/number-field/index.tsx
index 94dccfcacce..f74905824b6 100644
--- a/packages/dify-ui/src/number-field/index.tsx
+++ b/packages/dify-ui/src/number-field/index.tsx
@@ -98,7 +98,7 @@ function NumberFieldUnit({ className, size = 'medium', ...props }: NumberFieldUn
}
const numberFieldControlsVariants = cva(
- 'flex w-6 shrink-0 flex-col items-stretch border-l border-divider-subtle bg-transparent text-text-tertiary',
+ 'flex w-6 shrink-0 flex-col items-stretch border-s border-divider-subtle bg-transparent text-text-tertiary',
)
type NumberFieldControlsProps = React.ComponentProps<'div'>
diff --git a/packages/dify-ui/src/pagination/index.tsx b/packages/dify-ui/src/pagination/index.tsx
index 5eb9cd4e3a1..9245cdec8ec 100644
--- a/packages/dify-ui/src/pagination/index.tsx
+++ b/packages/dify-ui/src/pagination/index.tsx
@@ -251,7 +251,12 @@ function PaginationPrevious({
if (!event.defaultPrevented && !disabled) pagination.onPageChange(pagination.page - 1)
}}
>
- {children ?? <span className="i-ri-arrow-left-line size-4" aria-hidden="true" />}
+ {children ?? (
+ <span
+ className="i-ri-arrow-left-line size-4 [&:dir(rtl)]:-scale-x-100"
+ aria-hidden="true"
+ />
+ )}
</BaseButton>
)
}
@@ -281,7 +286,12 @@ function PaginationNext({
if (!event.defaultPrevented && !disabled) pagination.onPageChange(pagination.page + 1)
}}
>
- {children ?? <span className="i-ri-arrow-right-line size-4" aria-hidden="true" />}
+ {children ?? (
+ <span
+ className="i-ri-arrow-right-line size-4 [&:dir(rtl)]:-scale-x-100"
+ aria-hidden="true"
+ />
+ )}
</BaseButton>
)
}
diff --git a/packages/dify-ui/src/styles/components.css b/packages/dify-ui/src/styles/components.css
index f889ead1fe8..7e57b05b876 100644
--- a/packages/dify-ui/src/styles/components.css
+++ b/packages/dify-ui/src/styles/components.css
@@ -44,7 +44,7 @@
[data-dify-scroll-area-scrollbar][data-orientation='horizontal']::before {
top: 50%;
- left: 4px;
+ inset-inline-start: 4px;
width: 12px;
height: 4px;
transform: translateY(-50%);
@@ -53,13 +53,21 @@
[data-dify-scroll-area-scrollbar][data-orientation='horizontal']::after {
top: 50%;
- right: 4px;
+ inset-inline-end: 4px;
width: 12px;
height: 4px;
transform: translateY(-50%);
background: linear-gradient(to left, var(--color-components-panel-bg), transparent);
}
+[data-dify-scroll-area-scrollbar][data-orientation='horizontal']:dir(rtl)::before {
+ background: linear-gradient(to left, var(--color-components-panel-bg), transparent);
+}
+
+[data-dify-scroll-area-scrollbar][data-orientation='horizontal']:dir(rtl)::after {
+ background: linear-gradient(to right, var(--color-components-panel-bg), transparent);
+}
+
[data-dify-scroll-area-scrollbar][data-orientation='vertical']:not(
[data-overflow-y-start]
)::before {
diff --git a/packages/dify-ui/src/switch/index.stories.tsx b/packages/dify-ui/src/switch/index.stories.tsx
index 70da2c8f249..f22f3537548 100644
--- a/packages/dify-ui/src/switch/index.stories.tsx
+++ b/packages/dify-ui/src/switch/index.stories.tsx
@@ -2,6 +2,7 @@ import type { Meta, StoryObj } from '@storybook/react-vite'
import * as React from 'react'
import { expect } from 'storybook/test'
import { Switch, SwitchSkeleton } from '.'
+import { DirectionProvider } from '../direction-provider'
import { Field, FieldDescription, FieldLabel } from '../field'
const meta = {
@@ -260,6 +261,100 @@ export const SizeComparison: Story = {
render: () => <SizeComparisonDemo />,
}
+function RTLDemo() {
+ return (
+ <DirectionProvider direction="rtl">
+ <div lang="ar" dir="rtl" className="grid w-80 gap-6">
+ <div className="grid gap-3">
+ {(['xs', 'sm', 'md', 'lg'] as const).map((size) => (
+ <Field key={size} name={`rtl-${size}`}>
+ <FieldLabel className="flex items-center justify-between gap-3">
+ <span>{`تبديل (${size})`}</span>
+ <Switch size={size} defaultChecked={false} />
+ </FieldLabel>
+ </Field>
+ ))}
+ </div>
+ <div className="grid gap-3">
+ {(['md', 'lg'] as const).flatMap((size) =>
+ [false, true].map((checked) => (
+ <Field key={`${size}-${checked}`} name={`rtl-loading-${size}-${checked}`}>
+ <FieldLabel className="flex items-center justify-between gap-3">
+ <span>{`جارٍ التحميل — ${checked ? 'مفعّل' : 'متوقف'} (${size})`}</span>
+ <Switch size={size} checked={checked} loading />
+ </FieldLabel>
+ </Field>
+ )),
+ )}
+ </div>
+ </div>
+ </DirectionProvider>
+ )
+}
+
+export const RTL: Story = {
+ render: () => <RTLDemo />,
+ parameters: {
+ docs: {
+ story: { autoplay: false },
+ description: {
+ story:
+ 'Set both DirectionProvider and HTML dir to rtl. Checked thumbs move left and stay inside the track in every size. Loading indicators remain opposite the thumb in md and lg sizes.',
+ },
+ },
+ },
+ play: async ({ canvas, userEvent }) => {
+ for (const size of ['xs', 'sm', 'md', 'lg']) {
+ const control = canvas.getByRole('switch', { name: `تبديل (${size})` })
+ const thumb = control.querySelector('span')!
+ const trackBounds = control.getBoundingClientRect()
+ const uncheckedBounds = thumb.getBoundingClientRect()
+
+ await expect(control).not.toBeChecked()
+ await expect(uncheckedBounds.right).toBeLessThanOrEqual(trackBounds.right)
+ await userEvent.click(control)
+ await expect(control).toBeChecked()
+ await Promise.all(
+ control.getAnimations({ subtree: true }).map((animation) => animation.finished),
+ )
+ const checkedBounds = thumb.getBoundingClientRect()
+ await expect(checkedBounds.left).toBeLessThan(uncheckedBounds.left)
+ await expect(checkedBounds.left).toBeGreaterThanOrEqual(trackBounds.left)
+ await expect(checkedBounds.right).toBeLessThanOrEqual(trackBounds.right)
+
+ await userEvent.keyboard(' ')
+ await expect(control).not.toBeChecked()
+ await Promise.all(
+ control.getAnimations({ subtree: true }).map((animation) => animation.finished),
+ )
+ await expect(thumb.getBoundingClientRect().left).toBeCloseTo(uncheckedBounds.left)
+ }
+
+ for (const size of ['md', 'lg']) {
+ for (const state of ['off', 'on']) {
+ const control = canvas.getByRole('switch', {
+ name: `جارٍ التحميل — ${state === 'on' ? 'مفعّل' : 'متوقف'} (${size})`,
+ })
+ await expect(control).toHaveAttribute('aria-disabled', 'true')
+ await expect(control).toHaveAttribute('aria-checked', String(state === 'on'))
+ const [thumb, spinner] = control.querySelectorAll('span')
+ const trackBounds = control.getBoundingClientRect()
+ const thumbBounds = thumb!.getBoundingClientRect()
+ const spinnerBounds = spinner!.getBoundingClientRect()
+ for (const bounds of [thumbBounds, spinnerBounds]) {
+ await expect(bounds.left).toBeGreaterThanOrEqual(trackBounds.left)
+ await expect(bounds.right).toBeLessThanOrEqual(trackBounds.right)
+ }
+ await expect(
+ state === 'on'
+ ? thumbBounds.right <= spinnerBounds.left
+ : spinnerBounds.right <= thumbBounds.left,
+ ).toBe(true)
+ }
+ }
+ },
+}
+
function LoadingDemo() {
const [loading, setLoading] = React.useState(true)
diff --git a/packages/dify-ui/src/switch/index.tsx b/packages/dify-ui/src/switch/index.tsx
index 2900272f27e..f3c6ec44b2f 100644
--- a/packages/dify-ui/src/switch/index.tsx
+++ b/packages/dify-ui/src/switch/index.tsx
@@ -36,10 +36,10 @@ const switchThumbVariants = cva(
{
variants: {
size: {
- xs: 'h-1.5 w-1 rounded-[1px] data-checked:translate-x-1.5',
- sm: 'h-2 w-1.75 rounded-xs data-checked:translate-x-2.25',
- md: 'h-3 w-2.5 rounded-[3px] data-checked:translate-x-3.5',
- lg: 'size-3.5 rounded-sm data-checked:translate-x-4',
+ xs: 'h-1.5 w-1 rounded-[1px] data-checked:translate-x-1.5 data-checked:[&:dir(rtl)]:-translate-x-1.5',
+ sm: 'h-2 w-1.75 rounded-xs data-checked:translate-x-2.25 data-checked:[&:dir(rtl)]:-translate-x-2.25',
+ md: 'h-3 w-2.5 rounded-[3px] data-checked:translate-x-3.5 data-checked:[&:dir(rtl)]:-translate-x-3.5',
+ lg: 'size-3.5 rounded-sm data-checked:translate-x-4 data-checked:[&:dir(rtl)]:-translate-x-4',
},
},
defaultVariants: {
@@ -48,14 +48,17 @@ const switchThumbVariants = cva(
},
)
-const switchSpinnerVariants = cva('absolute top-1/2 -translate-x-1/2 -translate-y-1/2', {
- variants: {
- size: {
- md: 'left-[calc(50%+6px)] size-2 group-data-checked:left-[calc(50%-6px)]',
- lg: 'left-[calc(50%+8px)] size-2.5 group-data-checked:left-[calc(50%-8px)]',
+const switchSpinnerVariants = cva(
+ 'absolute top-1/2 -translate-x-1/2 -translate-y-1/2 [&:dir(rtl)]:translate-x-1/2',
+ {
+ variants: {
+ size: {
+ md: 'start-[calc(50%+6px)] size-2 group-data-checked:start-[calc(50%-6px)]',
+ lg: 'start-[calc(50%+8px)] size-2.5 group-data-checked:start-[calc(50%-8px)]',
+ },
},
},
-})
+)
type ControlledSwitchProps = {
checked: boolean