Skip to content

Commit

Permalink
feat: rtl for tooltip, popper; cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
tomzemp committed Feb 7, 2024
1 parent a9e69c2 commit d16afce
Show file tree
Hide file tree
Showing 9 changed files with 61 additions and 19 deletions.
6 changes: 4 additions & 2 deletions components/chip/API.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,10 @@ import { Chip } from '@dhis2-ui/chip'
|dragging|boolean||||
|icon|element||||
|marginBottom|number|`4`||`margin-bottom` value, applied in `px`|
|marginLeft|number|`4`||`margin-left` value, applied in `px`|
|marginRight|number|`4`||`margin-right` value, applied in `px`|
|marginInlineStart|number|`4`||`margin-inline-start` value, applied in `px`|
|marginLeft|number|`4`||`margin-inline-start` value if marginInlineStart is not provided, applied in `px`|
|marginInlineEnd|number|`4`||`margin-inline-end` value, applied in `px`|
|marginRight|number|`4`||`margin-inline-end` value if marginInlineEnd is not provided, applied in `px`|
|marginTop|number|`4`||`margin-top` value, applied in `px`|
|overflow|boolean||||
|selected|boolean||||
Expand Down
10 changes: 6 additions & 4 deletions components/divider/src/divider.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,20 @@ import { colors, spacers } from '@dhis2/ui-constants'
import PropTypes from 'prop-types'
import React from 'react'

const Divider = ({ className, dataTest, dense, margin }) => {
let rtlMargin = margin
const flipMargin = (margin) => {
const splitMargin = margin.split(' ')
if (splitMargin?.length === 4) {
rtlMargin = [
return [
splitMargin[0],
splitMargin[3],
splitMargin[2],
splitMargin[1],
].join(' ')
}
return margin
}

const Divider = ({ className, dataTest, dense, margin }) => {
return (
<div className={className} data-test={dataTest}>
<style jsx>{`
Expand All @@ -29,7 +31,7 @@ const Divider = ({ className, dataTest, dense, margin }) => {
margin: ${dense ? `${spacers.dp4} 0` : margin};
}
:dir(rtl) {
margin: ${dense ? `${spacers.dp4} 0` : rtlMargin};
margin: ${dense ? `${spacers.dp4} 0` : flipMargin(margin)};
}
`}</style>
</div>
Expand Down
15 changes: 14 additions & 1 deletion components/popper/src/popper.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,16 @@ import { usePopper } from 'react-popper'
import { getReferenceElement } from './get-reference-element.js'
import { deduplicateModifiers } from './modifiers.js'

const flipPlacement = (placement) => {
if (placement.startsWith('right')) {
return placement.replace('right', 'left')
}
if (placement.startsWith('left')) {
return placement.replace('left', 'right')
}
return placement
}

const Popper = ({
children,
className,
Expand Down Expand Up @@ -32,7 +42,10 @@ const Popper = ({
const { styles, attributes } = usePopper(referenceElement, popperElement, {
strategy,
onFirstUpdate,
placement,
placement:
document.documentElement.dir === 'rtl'
? flipPlacement(placement)
: placement,
modifiers: deduplicatedModifiers,
})

Expand Down
19 changes: 18 additions & 1 deletion components/popper/src/popper.stories.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { sharedPropTypes } from '@dhis2/ui-constants'
import React, { useRef } from 'react'
import React, { useEffect, useRef } from 'react'
import { Popper } from './popper.js'

const description = `
Expand Down Expand Up @@ -143,3 +143,20 @@ export const VirtualElementRef = (args) => {
}
VirtualElementRef.args = { placement: 'left-end' }
VirtualElementRef.parameters = { docs: { source: { type: 'code' } } }

export const RTL = (args) => {
useEffect(() => {
document.documentElement.setAttribute('dir', 'rtl')
return () => {
document.documentElement.setAttribute('dir', 'ltr')
}
}, [])
return (
<div dir="rtl">
<span>If dir=rtl, `left` and `right` placement are reversed</span>
<Template {...args} placement="left" />
<br />
<Template {...args} placement="right-start" />
</div>
)
}
7 changes: 0 additions & 7 deletions components/switch/src/switch/switch-icons.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,13 +116,6 @@ export function SwitchRegular({ className }) {
// fill="#FFFFFF"
// fillRule="nonzero"
></path>
<path
transform="scale(-1, -1)"
d="M7.74451387,10.0285252 C7.39595738,10.1198564 7.12375034,10.3923519 7.03251575,10.7412777 C6.94128115,11.0902035 7.04521722,11.4612586 7.30437605,11.7118278 L10.2982384,14.7078028 C10.6875399,15.0973991 11.3185977,15.0973991 11.7078992,14.7078028 L16.695624,8.71585285 C16.9547828,8.46528367 17.0587189,8.09422851 16.9674843,7.74530271 C16.8762497,7.39637691 16.6040426,7.12388146 16.2554861,7.0325502 C15.9069296,6.94121893 15.5362672,7.04526513 15.2859632,7.30469855 L11.0030688,12.5910713 L8.71403676,10.3006735 C8.46373279,10.0412401 8.09307036,9.9371939 7.74451387,10.0285252 Z"
className="checkmarkRTL"
// fill="#FFFFFF"
// fillRule="nonzero"
></path>
<path
d="M11,20 C15.9705627,20 20,15.9705627 20,11 C20,6.02943725 15.9705627,2 11,2 C6.02943725,2 2,6.02943725 2,11 C2,15.9705627 6.02943725,20 11,20 Z"
className="handle handle-unchecked"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ describe('<TableDataCell>', () => {
const left = '200px'
const wrapper = shallow(<TableDataCell left={left} />)

expect(wrapper.html()).toContain(`left: ${left};`)
expect(wrapper.html()).toContain(`inset-inline-start: ${left};`)
})
it('accepts a muted prop', () => {
const wrapper = shallow(<TableDataCell muted />)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ describe('<TableHeaderCell>', () => {
const left = '200px'
const wrapper = shallow(<TableHeaderCell left={left} />)

expect(wrapper.html()).toContain(`left: ${left};`)
expect(wrapper.html()).toContain(`inset-inline-start: ${left};`)
})
it('accepts an muted prop', () => {
const wrapper = shallow(<TableHeaderCell muted />)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ export const TableDataCell = forwardRef(
<style jsx>{styles}</style>
<style jsx>{`
td {
insert-inline-start: ${left};
inset-inline-start: ${left};
text-align: ${align};
width: ${width};
background-color: ${backgroundColor || colors.white};
Expand Down
17 changes: 16 additions & 1 deletion components/tooltip/src/tooltip.stories.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React from 'react'
import React, { useEffect } from 'react'
import { Tooltip } from './tooltip.js'

const subtitle = 'Displayed when a user hovers over the element'
Expand Down Expand Up @@ -186,3 +186,18 @@ export const HidesWhenOutOfFrame = (args) => (
<p>{"I'm an extra paragraph"}</p>
</div>
)

export const RTLLeft = (args) => {
useEffect(() => {
document.documentElement.setAttribute('dir', 'rtl')
return () => {
document.documentElement.setAttribute('dir', 'ltr')
}
}, [])
return (
<div dir="rtl">
<Template {...args} />
</div>
)
}
RTLLeft.args = { placement: 'left' }

0 comments on commit d16afce

Please sign in to comment.