Skip to content

Commit

Permalink
Components utils: rtl() return type, rtl.watch() utility (#33882)
Browse files Browse the repository at this point in the history
Co-authored-by: sarayourfriend <[email protected]>
  • Loading branch information
ciampo and sarayourfriend authored Aug 4, 2021
1 parent 70c6a8a commit 8ef2008
Showing 1 changed file with 15 additions and 1 deletion.
16 changes: 15 additions & 1 deletion packages/components/src/utils/rtl.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ export const convertLTRToRTL = ( ltrStyles = {} ) => {
* @param {import('react').CSSProperties} ltrStyles Ltr styles. Converts and renders from ltr -> rtl styles, if applicable.
* @param {import('react').CSSProperties} [rtlStyles] Rtl styles. Renders if provided.
*
* @return {Function} A function to output CSS styles for Emotion's renderer
* @return {() => import('@emotion/react').SerializedStyles} A function to output CSS styles for Emotion's renderer
*/
export function rtl( ltrStyles = {}, rtlStyles ) {
return () => {
Expand All @@ -79,3 +79,17 @@ export function rtl( ltrStyles = {}, rtlStyles ) {
return isRTL() ? css( convertLTRToRTL( ltrStyles ) ) : css( ltrStyles );
};
}

/**
* Call this in the `useMemo` dependency array to ensure that subsequent renders will
* cause rtl styles to update based on the `isRTL` return value even if all other dependencies
* remain the same.
*
* @example
* const styles = useMemo( () => {
* return css`
* ${ rtl( { marginRight: '10px' } ) }
* `;
* }, [ rtl.watch() ] );
*/
rtl.watch = () => isRTL();

0 comments on commit 8ef2008

Please sign in to comment.