Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

JavaScript

Babel is required to process package imports. If you need a different preprocessor remove all packages first.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                #app
              
            
!

CSS

              
                *
*:after
  box-sizing border-box
  -webkit-tap-highlight-color transparent


body
  align-items center
  background #fff
  display flex
  justify-content center
  min-height 100vh
  flex-direction column
  font-family -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif
  cursor var(--cursor)

.hsl-slider
  --handle-size 50
  width 280px
  height 20px
  border 4px solid #111
  border-radius 4px
  position relative
  max-width 70vw
  cursor pointer
  background 'linear-gradient(90deg, hsl(0, %s, %s), hsl(36, %s, %s), hsl(72, %s, %s), hsl(108, %s, %s), hsl(144, %s, %s), hsl(180, %s, %s), hsl(216, %s, %s), hsl(252, %s, %s), hsl(288, %s, %s), hsl(324, %s, %s), hsl(360, %s, %s))' % (calc(var(--saturation, 100) * 1%) calc(var(--lightness, 50) * 1%) calc(var(--saturation, 100) * 1%) calc(var(--lightness, 50) * 1%) calc(var(--saturation, 100) * 1%) calc(var(--lightness, 50) * 1%) calc(var(--saturation, 100) * 1%) calc(var(--lightness, 50) * 1%) calc(var(--saturation, 100) * 1%) calc(var(--lightness, 50) * 1%) calc(var(--saturation, 100) * 1%) calc(var(--lightness, 50) * 1%) calc(var(--saturation, 100) * 1%) calc(var(--lightness, 50) * 1%) calc(var(--saturation, 100) * 1%) calc(var(--lightness, 50) * 1%) calc(var(--saturation, 100) * 1%) calc(var(--lightness, 50) * 1%) calc(var(--saturation, 100) * 1%) calc(var(--lightness, 50) * 1%) calc(var(--saturation, 100) * 1%) calc(var(--lightness, 50) * 1%))

  &__arc
    height calc(var(--handle-size) * 2px + 4px)
    width calc(var(--handle-size) * 2px + 4px)
    position absolute
    top 50%
    left calc(var(--value, 0) * 1%)
    transform translate(-50%, -50%)
    border-radius 100%
    opacity 0.2
    border 4px solid #c6c6c6
    z-index -1
    -webkit-clip-path polygon(0 0, 100% 0, 50% 50%, 100% 100%, 0 100%, 50% 50%)
    clip-path polygon(0 0, 100% 0, 50% 50%, 100% 100%, 0 100%, 50% 50%)

  &__handle
    height calc(var(--handle-size) * 1px)
    width calc(var(--handle-size) * 1px)
    border-radius 100%
    background 'hsl(%s, %s, %s)' % (var(--hue, 0) calc(var(--saturation, 100) * 1%) calc(var(--lightness, 50) * 1%))
    border 4px solid #111
    touch-action none
    position absolute
    top 50%
    left calc(var(--value, 0) * 1%)
    transform translate(-50%, -50%)
    cursor var(--cursor)

    &--lightness
    &--saturation
      left 50%
      width 100%
      height 100%
      background transparent
      border 0
      transform translate(-50%, -50%) rotate(90deg) rotate(calc(var(--angle) * 1deg)) translate(0, calc(var(--handle-size) * 1px))
      &:before
        display none
      &:after
        content ''
        position absolute
        height 50%
        width 50%
        border 4px solid #111
        border-radius 100%
        top 50%
        left 50%
        transform translate(-50%, -50%)
    &--lightness:after
      background 'hsl(0, 0%, %s)' % (calc(var(--lightness, 50) * 1%))
    &--saturation:after
      background 'linear-gradient(90deg, hsl(0, %s, 50%), hsl(36, %s, 50%), hsl(72, %s, 50%), hsl(108, %s, 50%), hsl(144, %s, 50%), hsl(180, %s, 50%), hsl(216, %s, 50%), hsl(252, %s, 50%), hsl(288, %s, 50%), hsl(324, %s, 50%), hsl(360, %s, 50%))' % (calc(var(--saturation, 50) * 1%) calc(var(--saturation, 50) * 1%) calc(var(--saturation, 50) * 1%) calc(var(--saturation, 50) * 1%) calc(var(--saturation, 50) * 1%) calc(var(--saturation, 50) * 1%) calc(var(--saturation, 50) * 1%) calc(var(--saturation, 50) * 1%) calc(var(--saturation, 50) * 1%) calc(var(--saturation, 50) * 1%) calc(var(--saturation, 50) * 1%))

.container
  position relative

.values
  font-size .5rem
  position absolute
  color #ccc
  top calc(100% + 100px)
              
            
!

JS

              
                const { React, ReactDOM, PropTypes, w3color } = window
const { useEffect, useState, useRef } = React
const { render } = ReactDOM
const rootNode = document.getElementById('app')

/**
 * Grab the angle between two points
 * @param {Object} event - pointer event for current coords
 * @param {Object} element - element to use as center point
 * @param {Number} buffer - buffer so that angle doesn't allow handle + track overlap
 */
const getAngle = (event, element, buffer) => {
  const { clientX: x, clientY: y } =
    event.touches && event.touches.length ? event.touches[0] : event
  const {
    x: handleX,
    y: handleY,
    width: handleWidth,
    height: handleHeight,
  } = element.getBoundingClientRect()
  const handleCenterPoint = {
    x: handleX + handleWidth / 2,
    y: handleY + handleHeight / 2,
  }
  const angle =
    (Math.atan2(handleCenterPoint.y - y, handleCenterPoint.x - x) * 180) /
    Math.PI
  return Math.max(buffer, Math.min(180 - buffer, Math.abs(angle)))
}

const getInitialAngle = (value, buffer) => {
  return ((180 - buffer * 2) / 100) * value + buffer
}

const HslSlider = ({
  hue: propsHue = 180,
  saturation: propsSaturation = 100,
  lightness: propsLightness = 50,
  handleSize = 50,
  BUFFER = 40,
  onChange,
}) => {
  const [hue, setHue] = useState(propsHue)
  const [cursor, setCursor] = useState('grab')
  const [lightness, setLightness] = useState(propsLightness)
  const [saturation, setSaturation] = useState(propsSaturation)
  const [saturationAngle, setSaturationAngle] = useState(
    getInitialAngle(saturation, BUFFER)
  )
  const [lightnessAngle, setLightnessAngle] = useState(
    180 + (180 - getInitialAngle(lightness, BUFFER))
  )
  const handleRef = useRef(null)
  const trackRef = useRef(null)

  /**
   * Updates the hue based on the pointer position
   * @param {Number} x - Where pointer is in relation to hue track
   */
  const updateHue = e => {
    // Return if we are tapping a handle
    if (e.target.dataset.hslSliderHandle) return
    const { clientX: x } = e.touches && e.touches.length ? e.touches[0] : e
    const {
      left: trackLeft,
      width: trackWidth,
    } = trackRef.current.getBoundingClientRect()
    const newValue = (x - trackLeft) / trackWidth
    setHue(Math.max(0, Math.min(360, newValue * 360)))
  }

  /**
   *
   * @param {Object} event - pointer event
   */
  const updateLightness = event => {
    const angle = getAngle(event, handleRef.current, BUFFER)
    const lightness = ((angle - BUFFER) / (180 - BUFFER * 2)) * 100
    setLightnessAngle(180 + (180 - angle))
    setLightness(lightness)
  }

  const updateSaturation = event => {
    const angle = getAngle(event, handleRef.current, BUFFER)
    const saturation = ((angle - BUFFER) / (180 - BUFFER * 2)) * 100
    setSaturation(saturation)
    setSaturationAngle(angle)
  }

  /**
   * Convenience method to handle event listening on handles
   * This will remove the event listeners on pointerUp
   * @param {Function} onMove - onMove handler for handle
   */
  const handleUp = onMove => {
    const up = () => {
      setCursor('grab')
      document.documentElement.style.setProperty('--cursor', 'initial')
      window.removeEventListener('mousemove', onMove)
      window.removeEventListener('touchmove', onMove)
      window.removeEventListener('mouseup', up)
      window.removeEventListener('touchend', up)
    }
    return up
  }

  /**
   * Convenience method to assign event listening for handles
   * @param {Function} onMove - onMove handler for handle
   * @param {Bool} stopPropagation - defines whether to stop event propagation
   */
  const handleDown = (onMove, stopPropagation) => e => {
    if (stopPropagation) e.stopPropagation()
    setCursor('grabbing')
    document.documentElement.style.setProperty('--cursor', 'grabbing')
    window.addEventListener('mousemove', onMove)
    window.addEventListener('touchmove', onMove)
    window.addEventListener('mouseup', handleUp(onMove))
    window.addEventListener('touchend', handleUp(onMove))
  }

  useEffect(() => {
    if (onChange) onChange({ hue, saturation, lightness })
  }, [hue, saturation, lightness])

  useEffect(() => {
    trackRef.current.addEventListener('click', updateHue)
    return () => {
      trackRef.current.removeEventListener('click', updateHue)
    }
  }, [])

  return (
    <div
      ref={trackRef}
      className="hsl-slider"
      style={{
        '--handle-size': handleSize,
        '--lightness': lightness,
        '--saturation': saturation,
      }}>
      <div
        className="hsl-slider__arc"
        style={{
          '--value': Math.max(0, Math.min(100, (hue / 360) * 100)),
        }}
      />
      <div
        className="hsl-slider__handle"
        ref={handleRef}
        style={{
          '--cursor': cursor,
          '--value': Math.max(0, Math.min(100, (hue / 360) * 100)),
          '--hue': hue,
        }}
        onMouseDown={handleDown(updateHue)}
        onTouchStart={handleDown(updateHue)}
        title="Set hue">
        <div
          data-hsl-slider-handle="true"
          className="hsl-slider__handle hsl-slider__handle--saturation"
          onMouseDown={handleDown(updateSaturation, true)}
          onTouchStart={handleDown(updateSaturation, true)}
          style={{ '--angle': saturationAngle }}
          title="Set saturation"
        />
        <div
          data-hsl-slider-handle="true"
          className="hsl-slider__handle hsl-slider__handle--lightness"
          onMouseDown={handleDown(updateLightness, true)}
          onTouchStart={handleDown(updateLightness, true)}
          style={{ '--angle': lightnessAngle }}
          title="Set lightness"
        />
      </div>
    </div>
  )
}
HslSlider.propTypes = {
  hue: PropTypes.number,
  handleSize: PropTypes.number,
  saturation: PropTypes.number,
  lightness: PropTypes.number,
  BUFFER: PropTypes.number,
  onChange: PropTypes.func,
}
const App = () => {
  const [color, setColor] = useState(new w3color('hsl(180, 100%, 50%'))
  const onChange = ({ hue, saturation, lightness }) => {
    setColor(new w3color(`hsl(${hue}, ${saturation}%), ${lightness}%`))
  }
  return (
    <div className="container" style={{ '--color': color.toHslString() }}>
      <HslSlider
        hue={color.hue}
        saturation={color.sat * 100}
        lightness={color.lightness * 100}
        onChange={onChange}
      />
      <div className="values">
        <h1>{color.toHslString()}</h1>
        <h1>{color.toHexString()}</h1>
        <h1>{color.toRgbString()}</h1>
      </div>
    </div>
  )
}
render(<App />, rootNode)

              
            
!
999px

Console