Skip to content
GitLab
Projects Groups Snippets
  • /
  • Help
    • Help
    • Support
    • Community forum
    • Submit feedback
    • Contribute to GitLab
  • Sign in / Register
  • B bootstrap
  • Project information
    • Project information
    • Activity
    • Labels
    • Members
  • Repository
    • Repository
    • Files
    • Commits
    • Branches
    • Tags
    • Contributors
    • Graph
    • Compare
  • Issues 263
    • Issues 263
    • List
    • Boards
    • Service Desk
    • Milestones
  • Merge requests 114
    • Merge requests 114
  • CI/CD
    • CI/CD
    • Pipelines
    • Jobs
    • Schedules
  • Deployments
    • Deployments
    • Environments
    • Releases
  • Packages and registries
    • Packages and registries
    • Package Registry
    • Infrastructure Registry
  • Monitor
    • Monitor
    • Incidents
  • Analytics
    • Analytics
    • Value stream
    • CI/CD
    • Repository
  • Wiki
    • Wiki
  • Snippets
    • Snippets
  • Activity
  • Graph
  • Create a new issue
  • Jobs
  • Commits
  • Issue Boards
Collapse sidebar
  • Bootstrap
  • bootstrap
  • Issues
  • #33694
Closed
Open
Issue created Apr 19, 2021 by GeoSot@GeoSotContributor

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
}
Assignee
Assign to
Time tracking