Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import * as React from 'react';
import styles from '@patternfly/react-styles/css/components/Drawer/drawer';
import { css } from '@patternfly/react-styles';
import { DrawerColorVariant, DrawerContext } from './Drawer';
import { formatBreakpointMods } from '../../helpers/util';
import { formatBreakpointMods, getLanguageDirection } from '../../helpers/util';
import { GenerateId } from '../../helpers/GenerateId/GenerateId';
import { FocusTrap } from '../../helpers/FocusTrap/FocusTrap';
import cssPanelMdFlexBasis from '@patternfly/react-tokens/dist/esm/c_drawer__panel_md_FlexBasis';
Expand Down Expand Up @@ -111,7 +111,7 @@ export const DrawerPanelContent: React.FunctionComponent<DrawerPanelContentProps
const calcValueNow = () => {
let splitterPos;
let drawerSize;
const isRTL = window.getComputedStyle(panel.current).getPropertyValue('direction') === 'rtl';
const isRTL = getLanguageDirection(panel.current) === 'rtl';

if (isInline && (position === 'end' || position === 'right')) {
if (isRTL) {
Expand Down Expand Up @@ -198,7 +198,7 @@ export const DrawerPanelContent: React.FunctionComponent<DrawerPanelContentProps
};

const handleControlMove = (e: MouseEvent | TouchEvent, controlPosition: number) => {
const isRTL = window.getComputedStyle(panel.current).getPropertyValue('direction') === 'rtl';
const isRTL = getLanguageDirection(panel.current) === 'rtl';

e.stopPropagation();
if (!isResizing) {
Expand Down Expand Up @@ -264,7 +264,7 @@ export const DrawerPanelContent: React.FunctionComponent<DrawerPanelContentProps
const callbackMouseUp = React.useCallback(handleMouseup, []);

const handleKeys = (e: React.KeyboardEvent) => {
const isRTL = window.getComputedStyle(panel.current).getPropertyValue('direction') === 'rtl';
const isRTL = getLanguageDirection(panel.current) === 'rtl';

const key = e.key;
if (
Expand Down
6 changes: 3 additions & 3 deletions packages/react-core/src/components/Nav/NavList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import styles from '@patternfly/react-styles/css/components/Nav/nav';
import { css } from '@patternfly/react-styles';
import AngleLeftIcon from '@patternfly/react-icons/dist/esm/icons/angle-left-icon';
import AngleRightIcon from '@patternfly/react-icons/dist/esm/icons/angle-right-icon';
import { isElementInView } from '../../helpers/util';
import { getLanguageDirection, isElementInView } from '../../helpers/util';
import { NavContext } from './Nav';
import { PageSidebarContext } from '../Page/PageSidebar';
import { getResizeObserver } from '../../helpers/resizeObserver';
Expand Down Expand Up @@ -112,7 +112,7 @@ class NavList extends React.Component<NavListProps> {

componentDidMount() {
this.observer = getResizeObserver(this.navList.current, this.handleScrollButtons, true);
this.direction = getComputedStyle(this.navList.current).getPropertyValue('direction');
this.direction = getLanguageDirection(this.navList.current);
this.handleScrollButtons();
}

Expand All @@ -121,7 +121,7 @@ class NavList extends React.Component<NavListProps> {
}

componentDidUpdate() {
this.direction = getComputedStyle(this.navList.current).getPropertyValue('direction');
this.direction = getLanguageDirection(this.navList.current);
}

render() {
Expand Down
3 changes: 2 additions & 1 deletion packages/react-core/src/components/Slider/Slider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { TextInput } from '../TextInput';
import { Tooltip } from '../Tooltip';
import cssSliderValue from '@patternfly/react-tokens/dist/esm/c_slider_value';
import cssFormControlWidthChars from '@patternfly/react-tokens/dist/esm/c_slider__value_c_form_control_width_chars';
import { getLanguageDirection } from '../../helpers/util';

/** Properties for creating custom steps in a slider. These properties should be passed in as
* an object within an array to the slider component's customSteps property.
Expand Down Expand Up @@ -125,7 +126,7 @@ export const Slider: React.FunctionComponent<SliderProps> = ({
let isRTL: boolean;

React.useEffect(() => {
isRTL = window.getComputedStyle(sliderRailRef.current).getPropertyValue('direction') === 'rtl';
isRTL = getLanguageDirection(sliderRailRef.current) === 'rtl';
});

React.useEffect(() => {
Expand Down
6 changes: 3 additions & 3 deletions packages/react-core/src/components/Tabs/Tabs.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { PickOptional } from '../../helpers/typeUtils';
import AngleLeftIcon from '@patternfly/react-icons/dist/esm/icons/angle-left-icon';
import AngleRightIcon from '@patternfly/react-icons/dist/esm/icons/angle-right-icon';
import PlusIcon from '@patternfly/react-icons/dist/esm/icons/plus-icon';
import { getUniqueId, isElementInView, formatBreakpointMods } from '../../helpers/util';
import { getUniqueId, isElementInView, formatBreakpointMods, getLanguageDirection } from '../../helpers/util';
import { TabContent } from './TabContent';
import { TabProps } from './Tab';
import { TabsContextProvider } from './TabsContext';
Expand Down Expand Up @@ -333,7 +333,7 @@ class Tabs extends React.Component<TabsProps, TabsState> {
if (canUseDOM) {
window.addEventListener('resize', this.handleScrollButtons, false);
}
this.direction = getComputedStyle(this.tabList.current).getPropertyValue('direction');
this.direction = getLanguageDirection(this.tabList.current);
// call the handle resize function to check if scroll buttons should be shown
this.handleScrollButtons();
}
Expand Down Expand Up @@ -381,7 +381,7 @@ class Tabs extends React.Component<TabsProps, TabsState> {
this.setState({ showScrollButtons: false });
}

this.direction = getComputedStyle(this.tabList.current).getPropertyValue('direction');
this.direction = getLanguageDirection(this.tabList.current);
}

render() {
Expand Down
16 changes: 1 addition & 15 deletions packages/react-core/src/helpers/Popper/Popper.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { Placement, Modifier } from './thirdparty/popper-core';
import { clearTimeouts } from '../util';
import { css } from '@patternfly/react-styles';
import '@patternfly/react-styles/css/components/Popper/Popper.css';
import { getLanguageDirection } from '../util';

const hash = {
left: 'right',
Expand Down Expand Up @@ -45,21 +46,6 @@ const getOppositePlacement = (placement: Placement): any =>
export const getOpacityTransition = (animationDuration: number) =>
`opacity ${animationDuration}ms cubic-bezier(.54, 1.5, .38, 1.11)`;

export const getLanguageDirection = (targetElement: HTMLElement) => {
const defaultDirection = 'ltr';
let direction = defaultDirection;

if (targetElement) {
direction = getComputedStyle(targetElement).getPropertyValue('direction');
}

if (['ltr', 'rtl'].includes(direction)) {
return direction as 'ltr' | 'rtl';
}

return defaultDirection;
};

export interface PopperProps {
/**
* Trigger reference element to which the popper is relatively placed to.
Expand Down
2 changes: 2 additions & 0 deletions packages/react-core/src/helpers/__mocks__/util.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
export const getUniqueId = () => 'unique_id_mock';

export const clearTimeouts = () => {};

export const getLanguageDirection = () => 'ltr';
22 changes: 22 additions & 0 deletions packages/react-core/src/helpers/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -522,3 +522,25 @@ export const clearTimeouts = (timeoutRefs: React.RefObject<any>[]) => {
}
});
};

/**
* Helper function to get the language direction of a given element, useful for figuring out if left-to-right
* or right-to-left specific logic should be applied.
*
* @param {HTMLElement} targetElement - Element the helper will get the language direction of
* @param {'ltr' | 'rtl'} defaultDirection - Language direction to assume if one can't be determined, defaults to 'ltr'
* @returns {'ltr' | 'rtl'} - The language direction of the target element
*/
export const getLanguageDirection = (targetElement: HTMLElement, defaultDirection: 'ltr' | 'rtl' = 'ltr') => {
if (!targetElement) {
return defaultDirection;
}

const computedDirection = getComputedStyle(targetElement).getPropertyValue('direction');

if (['ltr', 'rtl'].includes(computedDirection)) {
return computedDirection as 'ltr' | 'rtl';
}

return defaultDirection;
};