ant-design / pro-components

🏆 Use Ant Design like a Pro!
https://pro-components.antdigital.dev
MIT License
4.23k stars 1.35k forks source link

🧐[问题] RTL Direction #6197

Open hmz22 opened 1 year ago

hmz22 commented 1 year ago

提问前先看看:

https://github.com/ryanhanwu/How-To-Ask-Questions-The-Smart-Way/blob/main/README-zh_CN.md

🧐 问题描述

the sidebar menu RTL does not change how can RTL direction for pro layout. I used this:

<ConfigProvider direction="rtl">
....
</ConfigProvider>

but not work this component just work as children of ProLayout like this:

<PropLayout>
   <ConfigProvider direction="rtl">
   ....
   </ConfigProvider>
</ProLayout>

💻 示例代码

https://codesandbox.io/s/ji-chu-shi-yong-forked-qfopbi?file=/App.tsx

🚑 其他信息

"dependencies": {
    "@ant-design/icons": "^4.7.0",
    "@ant-design/pro-components": "^2.3.29",
    "@babel/core": "^7.20.2",
    "@emotion/css": "^11.10.5",
    "antd": "^4.24.1",
    "less": "^4.1.3",
    "react": "^18.2.0",
    "react-dom": "^18.2.0"
  },
  "devDependencies": {
    "@types/react": "^18.0.24",
    "@types/react-dom": "^18.0.8",
    "@vitejs/plugin-react": "^2.2.0",
    "typescript": "^4.6.4",
    "vite": "^3.2.3"
  }
chenshuai2144 commented 1 year ago

TAutomatically replying with ChatGPT can be attempted, but it cannot be guaranteed to be completely accurate and may not fully address all issues. Please feel free to reply if you have any further questions or concerns. 此回复基于 ChatGPT 自动生成,可以尝试下方案,官方员会在一定时间后继续继续处理。

您好,感谢您提出的问题。

根据您提供的信息,我们了解到您想在 antd pro-layout 的 ConfigProvider 中使用 direction 属性来实现 RTL 方向,但是 ConfigProvider 只起作用于其子组件。

这是因为 ConfigProvider 组件是用于设置全局配置的,它的作用域只限于其子组件。ant-design 官方提供了一种解决方案,即使用 ConfigProvider 外层套一个 div,然后设置 div 的 dir 属性为 rtl,这样子组件就可以继承父组件 div 的样式了,示例如下:

<div dir="rtl">
  <ConfigProvider>
    <ProLayout>
      ...
    </ProLayout>
  </ConfigProvider>
</div>

除此之外,还有一种方法可以实现 RTL 方向,即使用 CSS 的 direction 属性,如下所示:

.ant-layout-sider {
  direction: rtl;
}

将上述代码加入样式文件中即可。这种方式比较简洁,但是需要注意的是,不要将 direction 属性加到 .ant-layout.ant-layout-content 等其他父级元素上,因为这些元素中可能也有一些子元素需要使用 LTR 方向。

希望这些解决方案可以帮助您解决问题。如果您还有其他问题或疑问,请随时在 Github Issue 中提出。