Last active
October 14, 2019 15:51
-
-
Save wojtekmaj/93a1f35a1e277267635f67902b504fd5 to your computer and use it in GitHub Desktop.
Generate CSS text outline in styled-components.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function toPx(val) { | |
return typeof val === 'number' ? `${val}px` : val; | |
} | |
/** | |
* Creates a text outline of a given width and color. | |
* | |
* Sample usage in styled-components: | |
* ${props => textOutline('2px', props.color)} | |
* | |
* @param {String|Number} width Outline width, e.g. '2px' or 2 | |
* @param {String} color Outline color, e.g. 'red' | |
*/ | |
export function textOutline(width, color) { | |
const widthPx = toPx(width); | |
const positionBase = [`-${widthPx}`, 0, widthPx]; | |
const positions = positionBase.reduce((arr, x) => arr.concat(positionBase.map(y => `${x} ${y}`)), []); | |
const shadows = positions.map(position => `${position} ${color}`); | |
return `text-shadow: ${shadows.join(',')};`; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment