Generate a code of CSS Sprite Mixin to Clipboard in Sketch.
JavaScript
Latest commit a90155c Nov 20, 2016 @littlebusters merge #8
Permalink
Failed to load latest commit information.
Sketch CSS Sprite Mixin.sketchplugin/Contents/Sketch merge #8 Nov 20, 2016
README.md fix typo Sep 29, 2016
css-sprite-generator-ss.png mod README Aug 22, 2015

README.md

Sketch CSS Sprite Mixin

Generate a code of mixin for scss, less and stylus in Sketch. Code is copied to the clipboard when run the plugin.

Sprites name are group layer name of top-level, and the Sprite image name is an Artboard name.

Usage

  1. Select Artboard.
  2. Choose Plugins › Sketch Css Sprite Mixin › (Scss|Sass|Less|Stylus).
  3. Genarated code to Clipboard.

Screen Shot

SCSS

@mixin cssSprite( $spriteVals ) {
    width: nth( $spriteVals, 1 );
    height: nth( $spriteVals, 2 );
    background-repeat: no-repeat;
    background-image: url( #{ nth( $spriteVals, 3 ) } );
    background-position: nth( $spriteVals, 4 ) nth( $spriteVals, 5 );
    @media only screen and ( -webkit-min-device-pixel-ratio: 2 ), only screen and ( min-device-pixel-ratio: 2 ) {
        background-image: url( #{ nth( $spriteVals, 6 ) } );
        background-size: $bgiSizeW $bgiSizeH;
    }
}
$icon-spritePath: '../img/icon-sprite';
$icon-spriteURL: $icon-spritePath + '.png';
$icon-spritex2URL: $icon-spritePath + '@2.png';
$bgiSizeW: 200px;
$bgiSizeH: 200px;
$instagram: 64px 64px $icon-spriteURL -100px -100px $icon-spritex2URL;
$pinterest: 64px 64px $icon-spriteURL 0px -100px $icon-spritex2URL;
$facebook: 64px 64px $icon-spriteURL -100px 0px $icon-spritex2URL;
$twitter: 64px 64px $icon-spriteURL 0px 0px $icon-spritex2URL;
$icon-spriteList: $instagram instagram, $pinterest pinterest, $facebook facebook, $twitter twitter;
@each $icon in $icon-spriteList {
    $icon-spriteName: nth( $icon, 2 );
    $icon-spriteClass: nth( $icon, 1 );
    .icon-#{$icon-spriteName} {
        @include cssSprite( $icon-spriteClass );
    }
}

Sass

=cssSprite( $spriteVals )
    width: nth( $spriteVals, 1 )
    height: nth( $spriteVals, 2 )
    background-repeat: no-repeat
    background-image: url( #{ nth( $spriteVals, 3 ) } )
    background-position: nth( $spriteVals, 4 ) nth( $spriteVals, 5 )
    @media only screen and ( -webkit-min-device-pixel-ratio: 2 ), only screen and ( min-device-pixel-ratio: 2 ) 
        background-image: url( #{ nth( $spriteVals, 6 ) } )
        background-size: $bgiSizeW $bgiSizeH


$icon-spritePath: '../img/icon-sprite'
$icon-spriteURL: $icon-spritePath + '.png'
$icon-spritex2URL: $icon-spritePath + '@2.png'
$bgiSizeW: 200px
$bgiSizeH: 200px
$instagram: 64px 64px $icon-spriteURL -100px -100px $icon-spritex2URL
$pinterest: 64px 64px $icon-spriteURL 0px -100px $icon-spritex2URL
$facebook: 64px 64px $icon-spriteURL -100px 0px $icon-spritex2URL
$twitter: 64px 64px $icon-spriteURL 0px 0px $icon-spritex2URL
$icon-spriteList: $instagram instagram, $pinterest pinterest, $facebook facebook, $twitter twitter
@each $icon in $icon-spriteList 
    $icon-spriteName: nth( $icon, 2 )
    $icon-spriteClass: nth( $icon, 1 )
    .icon-#{$icon-spriteName} 
        +cssSprite( $icon-spriteClass )

Less

.cssSprite( @spriteVals ) {
    width: extract( @spriteVals, 1 );
    height: extract( @spriteVals, 2 );
    background-repeat: no-repeat;
    background-image: e( %( 'url(%s)', extract( @spriteVals, 3 ) ) );
    background-position: extract( @spriteVals, 4 ) extract( @spriteVals, 5 );
    @media only screen and ( -webkit-min-device-pixel-ratio: 2 ), only screen and ( min-device-pixel-ratio: 2 ) {
        background-image: e( %( 'url(%s)', extract( @spriteVals, 6 ) ) );
        background-size: @bgiSizeW @bgiSizeH;
    }
}
@icon-spritePath: '../img/icon-sprite';
@icon-spriteURL: '@{icon-spritePath}.png';
@icon-spritex2URL: '@{icon-spritePath}@2.png';
@bgiSizeW: 200px;
@bgiSizeH: 200px;
@instagram: 64px 64px @icon-spriteURL -100px -100px @icon-spritex2URL;
@pinterest: 64px 64px @icon-spriteURL 0px -100px @icon-spritex2URL;
@facebook: 64px 64px @icon-spriteURL -100px 0px @icon-spritex2URL;
@twitter: 64px 64px @icon-spriteURL 0px 0px @icon-spritex2URL;
@icon-spriteList: @instagram instagram, @pinterest pinterest, @facebook facebook, @twitter twitter;
@spriteCounter: length( @icon-spriteList );
.cssSpriteBuilder( @spriteCounter ) when ( @spriteCounter > 0 ) {
    .cssSpriteBuilder(( @spriteCounter - 1 ));
    @icon: extract( @icon-spriteList, @spriteCounter );
    @icon-spriteName: extract( @icon, 2 );
    @icon-spriteClass: extract( @icon, 1 );
    .icon-@{icon-spriteName} {
        .cssSprite( @icon-spriteClass );
    }
}
.cssSpriteBuilder( @spriteCounter );

Stylus

cssSprite( $spriteVals )
    width: $spriteVals[0]
    height: $spriteVals[1]
    background-repeat: no-repeat
    background-image: url( $spriteVals[2] )
    background-position: $spriteVals[3] $spriteVals[4]
    @media only screen and ( -webkit-min-device-pixel-ratio: 2 ), only screen and ( min-device-pixel-ratio: 2 ) 
        background-image: url( $spriteVals[5] )
        background-size: $bgiSizeW $bgiSizeH


$icon-spritePath = '../img/icon-sprite'
$icon-spriteURL = $icon-spritePath + '.png'
$icon-spritex2URL = $icon-spritePath + '@2.png'
$bgiSizeW = 200px
$bgiSizeH = 200px
$instagram = 64px 64px $icon-spriteURL -100px -100px $icon-spritex2URL
$pinterest = 64px 64px $icon-spriteURL 0px -100px $icon-spritex2URL
$facebook = 64px 64px $icon-spriteURL -100px 0px $icon-spritex2URL
$twitter = 64px 64px $icon-spriteURL 0px 0px $icon-spritex2URL
$icon-spriteList = $instagram instagram, $pinterest pinterest, $facebook facebook, $twitter twitter
for $icon in $icon-spriteList 
    $icon-spriteName = $icon[1]
    $icon-spriteClass = $icon[0]
    .icon-{$icon-spriteName} 
        cssSprite( $icon-spriteClass )

Installation

You can find this plugin in "Sketch ToolBox.app". Type "mixin" on the search box, then you click "install" button.

Or, Download the plugin zip and unzip. You Choose Plugins › Reveal Plugins Folder... in Sketch.app. Put the unziped fonlder to the Plugins folder.

License

The MIT License (MIT)

(c) 2014

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.