PHP 8.2.30
Preview: triangle.js.flow Size: 3.01 KB
/proc/thread-self/root/home/byroehnu/.trash/node_modules11/polished/lib/mixins/triangle.js.flow

// @flow
import getValueAndUnit from '../helpers/getValueAndUnit'
import PolishedError from '../internalHelpers/_errors'

import type { SideKeyword } from '../types/sideKeyword'
import type { Styles } from '../types/style'
import type { TriangleConfiguration } from '../types/triangleConfiguration'

const getBorderWidth = (
  pointingDirection: SideKeyword,
  height: [number, string],
  width: [number, string],
): string => {
  const fullWidth = `${width[0]}${width[1] || ''}`
  const halfWidth = `${width[0] / 2}${width[1] || ''}`
  const fullHeight = `${height[0]}${height[1] || ''}`
  const halfHeight = `${height[0] / 2}${height[1] || ''}`

  switch (pointingDirection) {
    case 'top':
      return `0 ${halfWidth} ${fullHeight} ${halfWidth}`
    case 'topLeft':
      return `${fullWidth} ${fullHeight} 0 0`
    case 'left':
      return `${halfHeight} ${fullWidth} ${halfHeight} 0`
    case 'bottomLeft':
      return `${fullWidth} 0 0 ${fullHeight}`
    case 'bottom':
      return `${fullHeight} ${halfWidth} 0 ${halfWidth}`
    case 'bottomRight':
      return `0 0 ${fullWidth} ${fullHeight}`
    case 'right':
      return `${halfHeight} 0 ${halfHeight} ${fullWidth}`
    case 'topRight':
    default:
      return `0 ${fullWidth} ${fullHeight} 0`
  }
}

const getBorderColor = (pointingDirection: SideKeyword, foregroundColor: string): Object => {
  switch (pointingDirection) {
    case 'top':
    case 'bottomRight':
      return { borderBottomColor: foregroundColor }
    case 'right':
    case 'bottomLeft':
      return { borderLeftColor: foregroundColor }
    case 'bottom':
    case 'topLeft':
      return { borderTopColor: foregroundColor }
    case 'left':
    case 'topRight':
      return { borderRightColor: foregroundColor }

    default:
      throw new PolishedError(59)
  }
}

/**
 * CSS to represent triangle with any pointing direction with an optional background color.
 *
 * @example
 * // Styles as object usage
 *
 * const styles = {
 *   ...triangle({ pointingDirection: 'right', width: '100px', height: '100px', foregroundColor: 'red' })
 * }
 *
 *
 * // styled-components usage
 * const div = styled.div`
 *   ${triangle({ pointingDirection: 'right', width: '100px', height: '100px', foregroundColor: 'red' })}
 *
 *
 * // CSS as JS Output
 *
 * div: {
 *  'borderColor': 'transparent transparent transparent red',
 *  'borderStyle': 'solid',
 *  'borderWidth': '50px 0 50px 100px',
 *  'height': '0',
 *  'width': '0',
 * }
 */
export default function triangle({
  pointingDirection,
  height,
  width,
  foregroundColor,
  backgroundColor = 'transparent',
}: TriangleConfiguration): Styles {
  const widthAndUnit = getValueAndUnit(width)
  const heightAndUnit = getValueAndUnit(height)

  if (isNaN(heightAndUnit[0]) || isNaN(widthAndUnit[0])) {
    throw new PolishedError(60)
  }

  return {
    width: '0',
    height: '0',
    borderColor: backgroundColor,
    ...getBorderColor(pointingDirection, foregroundColor),
    borderStyle: 'solid',
    borderWidth: getBorderWidth(pointingDirection, heightAndUnit, widthAndUnit),
  }
}

Directory Contents

Dirs: 0 × Files: 48

Name Size Perms Modified Actions
162 B lrw-r--r-- 2026-02-28 00:31:10
Edit Download
2.54 KB lrw-r--r-- 2026-02-28 00:30:16
Edit Download
1.96 KB lrw-r--r-- 2026-02-28 00:28:52
Edit Download
120 B lrw-r--r-- 2026-02-28 00:31:12
Edit Download
703 B lrw-r--r-- 2026-02-28 00:30:22
Edit Download
627 B lrw-r--r-- 2026-02-28 00:29:00
Edit Download
123 B lrw-r--r-- 2026-02-28 00:31:12
Edit Download
699 B lrw-r--r-- 2026-02-28 00:30:24
Edit Download
649 B lrw-r--r-- 2026-02-28 00:29:04
Edit Download
179 B lrw-r--r-- 2026-02-28 00:31:14
Edit Download
1.39 KB lrw-r--r-- 2026-02-28 00:30:28
Edit Download
1.04 KB lrw-r--r-- 2026-02-28 00:29:12
Edit Download
299 B lrw-r--r-- 2026-02-28 00:31:14
Edit Download
4.46 KB lrw-r--r-- 2026-02-28 00:30:30
Edit Download
2.56 KB lrw-r--r-- 2026-02-28 00:29:16
Edit Download
412 B lrw-r--r-- 2026-02-28 00:31:14
Edit Download
4.11 KB lrw-r--r-- 2026-02-28 00:30:30
Edit Download
3.54 KB lrw-r--r-- 2026-02-28 00:29:18
Edit Download
105 B lrw-r--r-- 2026-02-28 00:31:14
Edit Download
719 B lrw-r--r-- 2026-02-28 00:30:34
Edit Download
692 B lrw-r--r-- 2026-02-28 00:29:30
Edit Download
113 B lrw-r--r-- 2026-02-28 00:31:14
Edit Download
1018 B lrw-r--r-- 2026-02-28 00:30:34
Edit Download
986 B lrw-r--r-- 2026-02-28 00:29:32
Edit Download
71 B lrw-r--r-- 2026-02-28 00:31:14
Edit Download
1.13 KB lrw-r--r-- 2026-02-28 00:30:34
Edit Download
1022 B lrw-r--r-- 2026-02-28 00:29:32
Edit Download
274 B lrw-r--r-- 2026-02-28 00:31:16
Edit Download
1.98 KB lrw-r--r-- 2026-02-28 00:30:40
Edit Download
1.59 KB lrw-r--r-- 2026-02-28 00:29:40
Edit Download
114 B lrw-r--r-- 2026-02-28 00:31:20
Edit Download
3.40 KB lrw-r--r-- 2026-02-28 00:30:44
Edit Download
3.73 KB lrw-r--r-- 2026-02-28 00:29:46
Edit Download
290 B lrw-r--r-- 2026-02-28 00:31:20
Edit Download
2.18 KB lrw-r--r-- 2026-02-28 00:30:56
Edit Download
1.62 KB lrw-r--r-- 2026-02-28 00:29:52
Edit Download
233 B lrw-r--r-- 2026-02-28 00:31:22
Edit Download
2.21 KB lrw-r--r-- 2026-02-28 00:30:58
Edit Download
1.58 KB lrw-r--r-- 2026-02-28 00:29:56
Edit Download
166 B lrw-r--r-- 2026-02-28 00:31:26
Edit Download
2.36 KB lrw-r--r-- 2026-02-28 00:31:00
Edit Download
2.38 KB lrw-r--r-- 2026-02-28 00:30:02
Edit Download
281 B lrw-r--r-- 2026-02-28 00:31:26
Edit Download
3.66 KB lrw-r--r-- 2026-02-28 00:31:02
Edit Download
3.01 KB lrw-r--r-- 2026-02-28 00:30:06
Edit Download
118 B lrw-r--r-- 2026-02-28 00:31:26
Edit Download
743 B lrw-r--r-- 2026-02-28 00:31:04
Edit Download
675 B lrw-r--r-- 2026-02-28 00:30:06
Edit Download

If ZipArchive is unavailable, a .tar will be created (no compression).