오른쪽에서 왼쪽(RTL) 지원
아랍어, 페르시아어, 히브리어와 같은 언어를 지원하기 위해 Material UI로 오른쪽에서 왼쪽(RTL) 텍스트를 구현하는 방법을 배워보세요.
출처: 문서
본문
설정 (Setup)
이 가이드는 아래 데모에 표시된 대로 Material UI에서 텍스트 기반 컴포넌트의 방향을 변경하여 RTL 언어를 지원하는 데 필요한 세 가지 단계를 설명합니다:
{{"demo": "RtlDemo.js"}}
1. HTML 방향 설정
사용 사례에 따라 텍스트 방향을 전역적으로(앱 전체) 또는 로컬로(개별 컴포넌트에 한정) 설정할 수 있습니다.
전역적으로 (Globally)
전역 텍스트 방향을 설정하려면 앱의 루트 <html>에 dir="rtl"을 추가하세요:
<html dir="rtl"></html>
루트 <html> 요소에 dir 속성을 직접 설정할 수 없다면, 페이지가 렌더링되기 전에 JavaScript API를 사용하는 우회 방법을 쓰세요:
document.documentElement.setAttribute('dir', 'rtl');
로컬로 (Locally)
텍스트 방향의 범위를 특정 요소와 그 자식으로 한정해야 한다면, 다른 HTML 요소나 React 컴포넌트에 dir="rtl" 속성을 추가하세요.
:::warning
React 포털을 사용하는 컴포넌트(Dialog 등)는 실제로 부모 DOM 트리 밖에서 렌더링되기 때문에 부모의 dir 속성을 상속하지 않습니다.
전역적으로 오른쪽에서 왼쪽으로 정의되어 있지 않다면, 이 컴포넌트들에는 dir 속성을 직접 적용해야 합니다:
<Box dir="rtl">
<Dialog /> // ❌ this Dialog will still be left-to-right (the default)
</Box>
<Box dir="rtl">
<Dialog dir="rtl" /> // ✅ this Dialog will be right-to-left as intended
</Box>
:::
2. 테마 방향 설정
createTheme() API를 사용해 테마 방향을 'rtl'로 설정하세요:
import { createTheme } from '@mui/material/styles';
const theme = createTheme({
direction: 'rtl',
});
3. RTL 스타일 플러그인 구성
다음 명령 중 하나로 @mui/stylis-plugin-rtl을 설치하세요:
npm install stylis @mui/stylis-plugin-rtl
pnpm add stylis @mui/stylis-plugin-rtl
yarn add stylis @mui/stylis-plugin-rtl
Emotion을 사용하는 경우
Emotion을 사용한다면, CacheProvider를 사용해 @mui/stylis-plugin-rtl의 rtlPlugin을 사용하는 새 캐시 인스턴스를 만들고 애플리케이션 트리 상단에 추가하세요:
import { CacheProvider } from '@emotion/react';
import createCache from '@emotion/cache';
import { prefixer } from 'stylis';
import rtlPlugin from '@mui/stylis-plugin-rtl';
// Create rtl cache
const rtlCache = createCache({
key: 'muirtl',
stylisPlugins: [prefixer, rtlPlugin],
});
function Rtl(props) {
return <CacheProvider value={rtlCache}>{props.children}</CacheProvider>;
}
styled-components를 사용하는 경우
styled-components를 사용한다면, StyleSheetManager를 사용하고 stylisPlugins 속성에 rtlPlugin을 제공하세요:
import { StyleSheetManager } from 'styled-components';
import rtlPlugin from '@mui/stylis-plugin-rtl';
function Rtl(props) {
return (
<StyleSheetManager stylisPlugins={[rtlPlugin]}>
{props.children}
</StyleSheetManager>
);
}
특정 컴포넌트에서 RTL 해제하기 (Opting out of RTL locally)
특정 컴포넌트에서 RTL을 끄려면 템플릿 리터럴 문법을 사용하고 /* @noflip */ 지시어를 추가하세요:
const LeftToRightTextInRtlApp = styled('div')`
/* @noflip */
text-align: left;
`;
{{"demo": "RtlOptOut.js"}}