Skip to content

Instantly share code, notes, and snippets.

@wojtekmaj
Last active October 14, 2019 15:51
Show Gist options
  • Save wojtekmaj/93a1f35a1e277267635f67902b504fd5 to your computer and use it in GitHub Desktop.
Save wojtekmaj/93a1f35a1e277267635f67902b504fd5 to your computer and use it in GitHub Desktop.
Generate CSS text outline in styled-components.
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