Skip to content

Commit

Permalink
Merge pull request #50 from styled-components/retina-image
Browse files Browse the repository at this point in the history
Add retinaImage mixin
  • Loading branch information
mxstbr committed Dec 16, 2016
2 parents 82f6f8a + 17819d7 commit 79c7cc5
Show file tree
Hide file tree
Showing 5 changed files with 323 additions and 14 deletions.
158 changes: 144 additions & 14 deletions docs/index.html
Expand Up @@ -174,6 +174,16 @@ <h3 class='mb0 no-anchor'>polished</h3>
</li>


<li><a
href='#retinaimage'
class="">
retinaImage

</a>

</li>


<li><a
href='#animation'
class="">
Expand Down Expand Up @@ -216,7 +226,7 @@ <h3 class='fl m0' id='clearfix'>
clearFix
</h3>

<a class='fr fill-darken0 round round pad1x quiet h5' href='https://github.com/styled-components/polished/blob/e9c4746c4222010e7769ea67a95fad3230a7eba6/src/mixins/clearFix.js#L26-L35'>
<a class='fr fill-darken0 round round pad1x quiet h5' href='https://github.com/styled-components/polished/blob/a50733b97729d260811504dbb1e0e40b318fa7f5/src/mixins/clearFix.js#L26-L35'>
<span>src/mixins/clearFix.js</span>
</a>

Expand Down Expand Up @@ -300,7 +310,7 @@ <h3 class='fl m0' id='ellipsis'>
ellipsis
</h3>

<a class='fr fill-darken0 round round pad1x quiet h5' href='https://github.com/styled-components/polished/blob/e9c4746c4222010e7769ea67a95fad3230a7eba6/src/mixins/ellipsis.js#L29-L38'>
<a class='fr fill-darken0 round round pad1x quiet h5' href='https://github.com/styled-components/polished/blob/a50733b97729d260811504dbb1e0e40b318fa7f5/src/mixins/ellipsis.js#L29-L38'>
<span>src/mixins/ellipsis.js</span>
</a>

Expand Down Expand Up @@ -387,7 +397,7 @@ <h3 class='fl m0' id='hidpi'>
hiDPI
</h3>

<a class='fr fill-darken0 round round pad1x quiet h5' href='https://github.com/styled-components/polished/blob/e9c4746c4222010e7769ea67a95fad3230a7eba6/src/mixins/hiDPI.js#L32-L40'>
<a class='fr fill-darken0 round round pad1x quiet h5' href='https://github.com/styled-components/polished/blob/a50733b97729d260811504dbb1e0e40b318fa7f5/src/mixins/hiDPI.js#L32-L40'>
<span>src/mixins/hiDPI.js</span>
</a>

Expand Down Expand Up @@ -477,7 +487,7 @@ <h3 class='fl m0' id='hidetext'>
hideText
</h3>

<a class='fr fill-darken0 round round pad1x quiet h5' href='https://github.com/styled-components/polished/blob/e9c4746c4222010e7769ea67a95fad3230a7eba6/src/mixins/hideText.js#L29-L35'>
<a class='fr fill-darken0 round round pad1x quiet h5' href='https://github.com/styled-components/polished/blob/a50733b97729d260811504dbb1e0e40b318fa7f5/src/mixins/hideText.js#L29-L35'>
<span>src/mixins/hideText.js</span>
</a>

Expand Down Expand Up @@ -550,7 +560,7 @@ <h3 class='fl m0' id='selection'>
selection
</h3>

<a class='fr fill-darken0 round round pad1x quiet h5' href='https://github.com/styled-components/polished/blob/e9c4746c4222010e7769ea67a95fad3230a7eba6/src/mixins/selection.js#L31-L40'>
<a class='fr fill-darken0 round round pad1x quiet h5' href='https://github.com/styled-components/polished/blob/a50733b97729d260811504dbb1e0e40b318fa7f5/src/mixins/selection.js#L31-L40'>
<span>src/mixins/selection.js</span>
</a>

Expand Down Expand Up @@ -646,7 +656,7 @@ <h3 class='fl m0' id='size'>
size
</h3>

<a class='fr fill-darken0 round round pad1x quiet h5' href='https://github.com/styled-components/polished/blob/e9c4746c4222010e7769ea67a95fad3230a7eba6/src/mixins/size.js#L24-L29'>
<a class='fr fill-darken0 round round pad1x quiet h5' href='https://github.com/styled-components/polished/blob/a50733b97729d260811504dbb1e0e40b318fa7f5/src/mixins/size.js#L24-L29'>
<span>src/mixins/size.js</span>
</a>

Expand Down Expand Up @@ -736,7 +746,7 @@ <h3 class='fl m0' id='timingfunctions'>
timingFunctions
</h3>

<a class='fr fill-darken0 round round pad1x quiet h5' href='https://github.com/styled-components/polished/blob/e9c4746c4222010e7769ea67a95fad3230a7eba6/src/mixins/timingFunctions.js#L81-L83'>
<a class='fr fill-darken0 round round pad1x quiet h5' href='https://github.com/styled-components/polished/blob/a50733b97729d260811504dbb1e0e40b318fa7f5/src/mixins/timingFunctions.js#L81-L83'>
<span>src/mixins/timingFunctions.js</span>
</a>

Expand Down Expand Up @@ -816,7 +826,7 @@ <h3 class='fl m0' id='wordwrap'>
wordWrap
</h3>

<a class='fr fill-darken0 round round pad1x quiet h5' href='https://github.com/styled-components/polished/blob/e9c4746c4222010e7769ea67a95fad3230a7eba6/src/mixins/wordWrap.js#L26-L33'>
<a class='fr fill-darken0 round round pad1x quiet h5' href='https://github.com/styled-components/polished/blob/a50733b97729d260811504dbb1e0e40b318fa7f5/src/mixins/wordWrap.js#L26-L33'>
<span>src/mixins/wordWrap.js</span>
</a>

Expand Down Expand Up @@ -915,7 +925,7 @@ <h3 class='fl m0' id='stripunit'>
stripUnit
</h3>

<a class='fr fill-darken0 round round pad1x quiet h5' href='https://github.com/styled-components/polished/blob/e9c4746c4222010e7769ea67a95fad3230a7eba6/src/helpers/stripUnit.js#L24-L28'>
<a class='fr fill-darken0 round round pad1x quiet h5' href='https://github.com/styled-components/polished/blob/a50733b97729d260811504dbb1e0e40b318fa7f5/src/helpers/stripUnit.js#L24-L28'>
<span>src/helpers/stripUnit.js</span>
</a>

Expand Down Expand Up @@ -1000,7 +1010,7 @@ <h3 class='fl m0' id='em'>
em
</h3>

<a class='fr fill-darken0 round round pad1x quiet h5' href='https://github.com/styled-components/polished/blob/e9c4746c4222010e7769ea67a95fad3230a7eba6/src/helpers/em.js#L29-L29'>
<a class='fr fill-darken0 round round pad1x quiet h5' href='https://github.com/styled-components/polished/blob/a50733b97729d260811504dbb1e0e40b318fa7f5/src/helpers/em.js#L29-L29'>
<span>src/helpers/em.js</span>
</a>

Expand Down Expand Up @@ -1090,7 +1100,7 @@ <h3 class='fl m0' id='modularscale'>
modularScale
</h3>

<a class='fr fill-darken0 round round pad1x quiet h5' href='https://github.com/styled-components/polished/blob/e9c4746c4222010e7769ea67a95fad3230a7eba6/src/helpers/modularScale.js#L66-L82'>
<a class='fr fill-darken0 round round pad1x quiet h5' href='https://github.com/styled-components/polished/blob/a50733b97729d260811504dbb1e0e40b318fa7f5/src/helpers/modularScale.js#L66-L82'>
<span>src/helpers/modularScale.js</span>
</a>

Expand Down Expand Up @@ -1190,7 +1200,7 @@ <h3 class='fl m0' id='rem'>
rem
</h3>

<a class='fr fill-darken0 round round pad1x quiet h5' href='https://github.com/styled-components/polished/blob/e9c4746c4222010e7769ea67a95fad3230a7eba6/src/helpers/rem.js#L28-L28'>
<a class='fr fill-darken0 round round pad1x quiet h5' href='https://github.com/styled-components/polished/blob/a50733b97729d260811504dbb1e0e40b318fa7f5/src/helpers/rem.js#L28-L28'>
<span>src/helpers/rem.js</span>
</a>

Expand Down Expand Up @@ -1280,7 +1290,7 @@ <h3 class='fl m0' id='placeholder'>
placeholder
</h3>

<a class='fr fill-darken0 round round pad1x quiet h5' href='https://github.com/styled-components/polished/blob/e9c4746c4222010e7769ea67a95fad3230a7eba6/src/mixins/placeholder.js#L35-L50'>
<a class='fr fill-darken0 round round pad1x quiet h5' href='https://github.com/styled-components/polished/blob/a50733b97729d260811504dbb1e0e40b318fa7f5/src/mixins/placeholder.js#L35-L50'>
<span>src/mixins/placeholder.js</span>
</a>

Expand Down Expand Up @@ -1367,6 +1377,126 @@ <h3 class='fl m0' id='placeholder'>



</section>




<section class='p2 mb2 clearfix bg-white minishadow'>


<div class='clearfix'>
<h3 class='fl m0' id='retinaimage'>
retinaImage
</h3>

<a class='fr fill-darken0 round round pad1x quiet h5' href='https://github.com/styled-components/polished/blob/a50733b97729d260811504dbb1e0e40b318fa7f5/src/mixins/retinaImage.js#L33-L48'>
<span>src/mixins/retinaImage.js</span>
</a>

</div>


<p>The retina-image mixin is a helper to generate a retina background image and non-retina
background image. The retina background image will output to a HiDPI media query. The mixin uses
a _2x.png filename suffix by default.</p>


<div class='pre p1 fill-light mt0'>retinaImage(filename: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String">string</a>, backgroundSize: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String">string</a>, extension: [<a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String">string</a>], retinaFilename: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String">string</a>, retinaSuffix: [<a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String">string</a>])</div>










<div class='py1 quiet mt1 prose-big'>Parameters</div>
<div class='prose'>

<div class='space-bottom0'>
<div>
<span class='code bold'>filename</span> <code class='quiet'>(<a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String">string</a>)</code>
</div>

</div>

<div class='space-bottom0'>
<div>
<span class='code bold'>backgroundSize</span> <code class='quiet'>(<a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String">string</a>)</code>
</div>

</div>

<div class='space-bottom0'>
<div>
<span class='code bold'>extension</span> <code class='quiet'>([<a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String">string</a>]
(default <code>&#39;png&#39;</code>)
)</code>
</div>

</div>

<div class='space-bottom0'>
<div>
<span class='code bold'>retinaFilename</span> <code class='quiet'>(<a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String">string</a>)</code>
</div>

</div>

<div class='space-bottom0'>
<div>
<span class='code bold'>retinaSuffix</span> <code class='quiet'>([<a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String">string</a>]
(default <code>&#39;_2x&#39;</code>)
)</code>
</div>

</div>

</div>









<div class='py1 quiet mt1 prose-big'>Example</div>


<pre class='p1 overflow-auto round fill-light'><span class="hljs-comment">// Styles as object usage</span>
<span class="hljs-keyword">const</span> styles = {
...retinaImage(<span class="hljs-string">'my-img'</span>)
}

<span class="hljs-comment">// styled-components usage</span>
<span class="hljs-keyword">const</span> div = styled.div`
${retinaImage(<span class="hljs-string">'my-img'</span>)}
`

<span class="hljs-comment">// CSS as JS Output</span>
div {
backgroundImage: 'url(my-img.png)',
'@media only screen and (-webkit-min-device-pixel-ratio: 1.3),
only screen and (min--moz-device-pixel-ratio: 1.3),
only screen and (-o-min-device-pixel-ratio: 1.3/1),
only screen and (min-resolution: 144dpi),
only screen and (min-resolution: 1.5dppx)': {
backgroundImage: 'url(my-img_2x.png)',
}
}</pre>








</section>


Expand All @@ -1380,7 +1510,7 @@ <h3 class='fl m0' id='animation'>
animation
</h3>

<a class='fr fill-darken0 round round pad1x quiet h5' href='https://github.com/styled-components/polished/blob/e9c4746c4222010e7769ea67a95fad3230a7eba6/src/shorthands/animation.js#L41-L61'>
<a class='fr fill-darken0 round round pad1x quiet h5' href='https://github.com/styled-components/polished/blob/a50733b97729d260811504dbb1e0e40b318fa7f5/src/shorthands/animation.js#L41-L61'>
<span>src/shorthands/animation.js</span>
</a>

Expand Down
3 changes: 3 additions & 0 deletions src/index.js
Expand Up @@ -11,6 +11,7 @@ import ellipsis from './mixins/ellipsis'
import hideText from './mixins/hideText'
import hiDPI from './mixins/hiDPI'
import placeholder from './mixins/placeholder'
import retinaImage from './mixins/retinaImage'
import selection from './mixins/selection'
import size from './mixins/size'
import timingFunctions from './mixins/timingFunctions'
Expand All @@ -29,6 +30,7 @@ const polished = {
hiDPI,
placeholder,
rem,
retinaImage,
selection,
size,
stripUnit,
Expand All @@ -48,6 +50,7 @@ export {
hiDPI,
placeholder,
rem,
retinaImage,
selection,
size,
stripUnit,
Expand Down
50 changes: 50 additions & 0 deletions src/mixins/retinaImage.js
@@ -0,0 +1,50 @@
// @flow

import hiDPI from './hiDPI'

/**
* The retina-image mixin is a helper to generate a retina background image and non-retina
* background image. The retina background image will output to a HiDPI media query. The mixin uses
* a _2x.png filename suffix by default.
*
* @example
* // Styles as object usage
* const styles = {
* ...retinaImage('my-img')
* }
*
* // styled-components usage
* const div = styled.div`
* ${retinaImage('my-img')}
* `
*
* // CSS as JS Output
* div {
* backgroundImage: 'url(my-img.png)',
* '@media only screen and (-webkit-min-device-pixel-ratio: 1.3),
* only screen and (min--moz-device-pixel-ratio: 1.3),
* only screen and (-o-min-device-pixel-ratio: 1.3/1),
* only screen and (min-resolution: 144dpi),
* only screen and (min-resolution: 1.5dppx)': {
* backgroundImage: 'url(my-img_2x.png)',
* }
* }
*/
function retinaImage(filename: string, backgroundSize?: string, extension?: string = 'png', retinaFilename?: string, retinaSuffix?: string = '_2x') {
if (!filename) {
throw new Error('Please supply a filename to retinaImage() as the first argument.')
}
// Replace the dot at the beginning of the passed extension if one exists
const ext = extension.replace(/^\./, '')
const rFilename = retinaFilename ? `${retinaFilename}.${ext}` : `${filename}${retinaSuffix}.${ext}`

return {
backgroundImage: `url(${filename}.${ext})`,
[hiDPI()]: {
backgroundImage: `url(${rFilename})`,
backgroundSize,
},
}
}

export default retinaImage

0 comments on commit 79c7cc5

Please sign in to comment.