Skip to content

Instantly share code, notes, and snippets.

@siddharthkp
Created December 4, 2018 05:50
Show Gist options
  • Select an option

  • Save siddharthkp/73a910f91212a10b21d53cb3a0b280d6 to your computer and use it in GitHub Desktop.

Select an option

Save siddharthkp/73a910f91212a10b21d53cb3a0b280d6 to your computer and use it in GitHub Desktop.

Revisions

  1. siddharthkp created this gist Dec 4, 2018.
    35 changes: 35 additions & 0 deletions temp.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,35 @@
    import { spacing } from '@auth0/cosmos-tokens'

    const directions = ['top', 'bottom', 'left', 'right']

    /* this helper gets all the props of the component */
    const margin = ({ margin = {} }) => {
    let styles = ''

    directions.map(direction => {
    if (typeof margin[direction] !== 'undefined') {
    let rawValue = margin[direction]
    let value

    /*
    if the value is a string, it's either a token (xsmall)
    or has units (8em)
    if it's a number, we assume it's pixels
    */

    if (typeof rawValue === 'string') {
    if (spacing[rawValue]) value = spacing[rawValue]
    else value = rawValue
    } else {
    value = rawValue + 'px'
    }

    // syntax: margin-top: margin.top
    styles += `margin-${direction}: ${value};`
    }
    })
    return styles
    }

    export default margin