1 import React, { useEffect, useState } from 'react';
3 import RightToLeftContext from './context';
5 const getIsRTL = (lang: string) => {
6 return /^fa/.test(lang);
10 children: React.ReactNode;
13 export const RightToLeftProvider = ({ children }: Props) => {
14 const state = useState(getIsRTL(document.documentElement.lang));
15 const [isRTL, setRTL] = state;
18 const observer = new MutationObserver((e) => {
19 if (e.some(({ attributeName }) => attributeName === 'lang')) {
20 setRTL(getIsRTL(document.documentElement.lang));
23 observer.observe(document.documentElement, { attributes: true });
25 observer.disconnect();
30 document.documentElement.dir = isRTL ? 'rtl' : 'ltr';
33 return <RightToLeftContext.Provider value={state}>{children}</RightToLeftContext.Provider>;