5

I am using gulp-spritesmith to generate my sprite, and I face a problem: I want some of the generated styles to be properties for hover rules, not for a class selectors. Adding class on mouseover event looks ugly, and I don't consider it as a solution.

For example:

.icon-sr_ext_icon_right {
  background-image: url(/imgs/static/external_sprite.png);
  background-position: -300px -100px;
  width: 50px;
  height: 50px;
}
.icon-sr_ext_icon_right_hovered {
  background-image: url(/imgs/static/external_sprite.png);
  background-position: -222px -200px;
  width: 50px;
  height: 50px;
}

To be:

.icon-sr_ext_icon_right {
  background-image: url(/imgs/static/external_sprite.png);
  background-position: -300px -100px;
  width: 50px;
  height: 50px;
}
.icon-sr_ext_icon_right:hover{
  background-image: url(/imgs/static/external_sprite.png);
  background-position: -222px -200px;
  width: 50px;
  height: 50px;
}

Here is my gulp task code:

gulp.task('external_sprite', function() {
    var spriteData =
        gulp.src(paths.external.sprite)
            .pipe(plugins.debug( { title: "Processing image for external sprite:" } ) )
            .pipe(plugins.spritesmith({
                imgName: 'external_sprite.png',
                imgPath: '/imgs/static/external_sprite.png',
                cssName: 'external_sprite.css'
            }));

    spriteData.img.pipe(gulp.dest('./www/imgs/static/'));
    spriteData.css.pipe(gulp.dest('./' + paths.external.src)); 
});
Alexandr Lazarev
  • 12,554
  • 4
  • 38
  • 47

1 Answers1

5

I've found a way how to automatically create styles for hover effects, using sass technology. Simply, generate sprite, than import generated css to another sass file and extend needed class:

@import 'external_sprite';
.icon-sr_ext_icon_right:hover {
    @extend .icon-sr_ext_icon_right_hovered;
}

Another way is suggested by the main contributor of the plugin in the issue on github. The idea is to use cssOpts.cssClass:

cssOpts: {
  cssSelector: function (item) {
    // If this is a hover sprite, name it as a hover one (e.g. 'home-hover' -> 'home:hover')
    if (item.name.indexOf('-hover') !== -1) {
      return '.sprite-' + item.name.replace('-hover', ':hover');
    // Otherwise, use the name as the selector (e.g. 'home' -> 'home')
    } else {
      return '.sprite-' + item.name;
    }
  }
}

But this solution is not working if you are setting .scss extension of the styles file.

Alexandr Lazarev
  • 12,554
  • 4
  • 38
  • 47
  • Great idea! But instead of importing the css i'm generating a scss sprite file but the idea remains the same – Pablo S G Pacheco Jan 28 '16 at 02:26
  • The gulp task code isn't working for me, I tried replacing cssClass with cssSelector but the entire cssOpts object is being ignored. No prefix is generated, class names end in -hover as the files do. What am I missing? – Ioan-Radu Tanasescu Jul 08 '16 at 11:51
  • It's hard tot tell so, could you provide your code? – Alexandr Lazarev Jul 08 '16 at 12:01
  • Sure, this is my sprite gulp task, names of files and generated css (after it's been ran through gulp-sass): https://jsfiddle.net/b38y9b6a/ – Ioan-Radu Tanasescu Jul 08 '16 at 12:06
  • https://gist.github.com/diedsmiling/16dee22f07d817f2ec36fe113aca470f - that worked for me, I've removed retina configs from your gulp task, and changed `.scss` extension to `.css`. Here a re my images http://take.ms/v9Wjd, here is css generated: http://take.ms/Kesqa – Alexandr Lazarev Jul 08 '16 at 12:28
  • 1
    Looks like sprites work too, it's just the scss part that is not working, simply using a .css termination makes the whole thing work, thank you. Now I need to figure out how to integrate that css with the rest of the scss – Ioan-Radu Tanasescu Jul 08 '16 at 12:42
  • Ok, probably it's worth opening an issue on github. You can also try to use my alternative solution - extending classes with sass. – Alexandr Lazarev Jul 08 '16 at 12:46
  • 1
    I don't think it's a bug, everything works if I just use the .css termination instead of the .scss one (retina works too). I think that's because it's cssOpts not scssOpts :) – Ioan-Radu Tanasescu Jul 08 '16 at 13:03
  • Can the scss classes be extended all automatically, not specifically one by one? (the example seems one by one manually typed) – Ioan-Radu Tanasescu Jul 08 '16 at 13:05
  • Issues aren't opened just for bugs :) It could be a proposal for an enhancement :) I've included them one by one, didn't had a lot of them. But I understand that it can be a problem with lots of icons. Have to think, what can be done. – Alexandr Lazarev Jul 08 '16 at 13:08
  • Thank you for everything, without your answer I would have spent a long time figuring out why it wasn't working. I'll open an issue, thank you! – Ioan-Radu Tanasescu Jul 08 '16 at 13:23
  • The solution to just use the generated css file instead of scss works fine for me, I guess most websites have more than one css file that get minimised and concatenated. – Ioan-Radu Tanasescu Jul 08 '16 at 13:24