Skip to content
This repository was archived by the owner on Feb 21, 2025. It is now read-only.

Commit 7f6b160

Browse files
committed
feat(sprite): add retina support
1 parent 6b0dee8 commit 7f6b160

File tree

3 files changed

+30
-2
lines changed

3 files changed

+30
-2
lines changed

docs/features/sprite.md

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
Uses [gulp.spritesmith](https://github.com/twolfson/gulp.spritesmith). Grabs a folder of images and turns them into a sprite, creates a Sass mixin and class for each based on filename.
22

3+
A documentation for SCSS features is available [here](https://www.bignerdranch.com/blog/css-sprite-management-with-gulp-part2/).
4+
35
## Usage
46

57
Given a file named `facebook.png`, you can use this Sass mixin:
@@ -11,9 +13,23 @@ Given a file named `facebook.png`, you can use this Sass mixin:
1113
Or this HTML class:
1214

1315
```html
14-
<span class="sprite_facebook"></span>
16+
<span class="sprite-facebook"></span>
1517
```
1618

19+
### Retina support
20+
21+
You can generate a second sprite for Retina.
22+
First, you need to duplicates all your images for Retina, and append "@2x" in the filename. For example: "facebook.png" and "[email protected]".
23+
24+
After enabled it, you can now use the mixin:
25+
26+
```scss
27+
@include retina-sprite('sprite-facebook');
28+
```
29+
30+
It will automatically take sprite@2x for Retina devices, and normal sprite for others.
31+
32+
1733
## Commands
1834

1935
- `gulp sprite` - Generates the sprite
@@ -29,5 +45,8 @@ Or this HTML class:
2945
- `config.sprite.imgPathPrefix` - Sprite image path prefix
3046
- `config.sprite.spritesheetName` - Name of the sprite
3147
- `config.sprite.imagemin` - Enable imagemin compression for the sprite image file
48+
- `config.sprite.retina.enabled` - Enable retina sprite generation
49+
- `config.sprite.retina.imgName` - Name of the retina sprite image file
50+
- `config.sprite.retina.filter` - Images filter that match the retina files
3251

3352
---

gulpfile.default.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,11 @@ sprite:
9797
imgPathPrefix: ../dist/
9898
spritesheetName: sprite
9999
imagemin: true
100+
retina:
101+
enabled: false
102+
103+
filter:
104+
- images/sprite/*@2x.png
100105
patternLab:
101106
enabled: false
102107
configFile: pattern-lab/config/config.yml

lib/sprite.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,13 @@ module.exports = (gulp, config, tasks) => {
1616
imgPath: `${config.sprite.imgPathPrefix}${config.sprite.imgName}`,
1717
cssName: config.sprite.cssName,
1818
cssSpritesheetName: config.sprite.spritesheetName,
19+
retinaSrcFilter: config.sprite.retina.enabled ? config.sprite.retina.filter : undefined,
20+
retinaImgName: config.sprite.retina.enabled ? config.sprite.retina.imgName : undefined,
21+
retinaImgPath: config.sprite.retina.enabled ? `${config.sprite.imgPathPrefix}${config.sprite.retina.imgName}` : undefined,
22+
cssRetinaSpritesheetName: config.sprite.retina.enabled ? `${config.sprite.spritesheetName}-2x` : undefined,
1923
cssVarMap(datas) {
2024
// eslint-disable-next-line no-param-reassign
21-
datas.name = `${config.sprite.spritesheetName}_${datas.name}`;
25+
datas.name = `${config.sprite.spritesheetName}-${datas.name}`;
2226
}
2327
}));
2428

0 commit comments

Comments
 (0)