Centralized Rtl handling
As RTL demands are growing, it would be nice for the name of clarity and sanity, to have a helper in one place, used by all js components
A draft of thoughts below:
const CLASSNAME_START = 'start'
const CLASSNAME_END = 'end'
const ORDER_NEXT = 'next'
const ORDER_PREV = 'prev'
const LEFT = 'left'
const RIGHT = 'right'
const UP = 'top'
const DOWN = 'right'
const isDirection = arg => [LEFT, RIGHT, UP, DOWN].includes(arg)
const isXaxisDirection = arg => [LEFT, RIGHT].includes(arg)
const isYaxisDirection = arg => isDirection(arg) && !isXaxisDirection(arg)
const isOrder = arg => [ORDER_NEXT, ORDER_PREV].includes(arg)
const orderToDirection = order => {
if (!isOrder(order)) {
return order
}
if (isRTL()) {
return order === ORDER_PREV ? LEFT : RIGHT
}
return order === ORDER_PREV ? RIGHT : LEFT
}
const directionToOrder = arg => {
if (!isDirection(arg) || isYaxisDirection(arg)) {
return arg
}
if (isRTL()) {
return arg === LEFT ? ORDER_PREV : ORDER_NEXT
}
return arg === LEFT ? ORDER_NEXT : ORDER_PREV
}
const sideToClass = (arg, prefix = '') => {
if (!isDirection(arg)) {
return arg
}
if (isYaxisDirection(arg)) {
return addPrefix(arg, prefix)
}
if (isRTL()) {
return addPrefix(arg === LEFT ? CLASSNAME_START : CLASSNAME_END, prefix)
}
return addPrefix(arg === LEFT ? CLASSNAME_END : CLASSNAME_START, prefix)
}
const addPrefix = (string, prefix) => {
return typeof prefix === 'string' && prefix !== '' ? `${prefix}-${string}` : string
}
export {
isDirection,
directionToOrder,
orderToDirection,
sideToClass
}