Top Banner
Drupal 8 Themes with SASS and SMACSS (and kittens)
50

Drupal 8 themes with SASS, SMACSS and kittens

Jul 16, 2015

Download

Internet

Liisa Duerig
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Drupal 8 themes with SASS, SMACSS and kittens

Drupal 8 Themes with

SASS and SMACSS

(and kittens)

Page 2: Drupal 8 themes with SASS, SMACSS and kittens
Page 3: Drupal 8 themes with SASS, SMACSS and kittens

Who am I?

Page 4: Drupal 8 themes with SASS, SMACSS and kittens
Page 5: Drupal 8 themes with SASS, SMACSS and kittens

Liisa, The CSS Nazi

Senior Software Developer, Super-

hyper Drupal Girl @Tieto

d.org: lduerig

Twitter: @LisKit7

Page 6: Drupal 8 themes with SASS, SMACSS and kittens

About Tieto

Tieto is the largest Nordic IT services provider, offering full

life-cycle services, mainly focusing in:

● Financial services

● Manufacturing, Retail and Logistics

● Public, Healthcare and Welfare

● Telecom, Media and Energy

Page 7: Drupal 8 themes with SASS, SMACSS and kittens

Not part of the scope of this presentation, but

ask if you want to know more. May be good for

a later presentation.

=

Page 8: Drupal 8 themes with SASS, SMACSS and kittens

Old school.

● media queries /

breakpoints

● sprites

● fixed gutter width

● fixed sprite width

Unmaintainable, manual

labour and prone to

inconsistencies.

Page 9: Drupal 8 themes with SASS, SMACSS and kittens

SASS - CSS with Superpowers

$variable + @mixin + @extend ==

less copy/paste ==

consistant ==

AWESOME!

(Be careful with @extends.

And nesting fever. (Later))

Page 10: Drupal 8 themes with SASS, SMACSS and kittens

What about Drupal?

● Can’t modules handle this?o Yes, but if you know enough code to be writing

SCSS, you probably shouldn’t need a module for it.

● How does this work with Drupal dev?o Perfectly. Separately.

Page 11: Drupal 8 themes with SASS, SMACSS and kittens

Setup

● Ruby

● Sass

● Compass

● Breakpoint

● Bundler, for version management

Page 12: Drupal 8 themes with SASS, SMACSS and kittens

Ruby

config.rb

● Environment (minification)

● Firesass

● Resources

● Compass plugins

● Import external stuff

● SASS options

Page 13: Drupal 8 themes with SASS, SMACSS and kittens

Bundler Gemfile

Useful if you are working on a pre-existing

setup, and need specific versions of things.

Page 14: Drupal 8 themes with SASS, SMACSS and kittens

Variables? Awesome!

Use for:

● breakpoints

● measurements

● font-sizes

● font-families

● … anything that you use often or should have a default.

Benefits:

● Consistent CSS.

● Easy to make site-wide changes.

● No need to sift through code if a breakpoint or font-size

should be changed.

● No need to remember what these default values are.

Page 15: Drupal 8 themes with SASS, SMACSS and kittens

Best practices

● Keep most site-wide variables in a partial _variables.scss, within your

SMACSS “base” directory.

● Use these variables in other partials, don’t mix up static styles with

variables!

o Every time I find font-size: 10px an SCSS partial, God kills a kitten and

I have nightmares.

● Vertical measurement variables and font-sizes should be ems and

calculated with Compass vertical-rhythm library.

● Horizontal measurement variables can be set however you want, perhaps

using multiples of a $base-unit-pixels. Be consistent.

$ compass watch….

Page 16: Drupal 8 themes with SASS, SMACSS and kittens

SASS @mixins

Use mixins for bits of code

that you use frequently in

your SCSS.

This little mixin makes it very

easy to whip up media

queries on-the-fly.

Page 17: Drupal 8 themes with SASS, SMACSS and kittens

And then the mixin grew up and realized life

wasn’t

so

simple.

Page 18: Drupal 8 themes with SASS, SMACSS and kittens

Don’t re-invent the wheel.

SASS libraries have a lot of awesome functions

and mixins to do this stuff for you.

Page 19: Drupal 8 themes with SASS, SMACSS and kittens

Breakpoint

http://breakpoint-sass.com

Compiles

to

Page 20: Drupal 8 themes with SASS, SMACSS and kittens

Breakpoint

http://breakpoint-sass.com

Really Simple, Organized,

Media Queries with Sass

Page 21: Drupal 8 themes with SASS, SMACSS and kittens

How about that obnoxious

sprite?

Page 22: Drupal 8 themes with SASS, SMACSS and kittens

Compass - Sprites

Files:images/my-icons/new.png

images/my-icons/edit.png

images/my-icons/save.png

images/my-icons/delete.png

SCSS:@import "compass/utilities/sprites";

@import "my-icons/*.png";

@include all-my-icons-sprites;

Generated CSS:.my-icons-sprite,

.my-icons-delete,

.my-icons-edit,

.my-icons-new,

.my-icons-save { background: url('/images/my-icons-

s34fe0604ab.png') no-repeat; }

.my-icons-delete { background-position: 0 0; }

.my-icons-edit { background-position: 0 -32px; }

.my-icons-new { background-position: 0 -64px; }

.my-icons-save { background-position: 0 -96px; }

Page 23: Drupal 8 themes with SASS, SMACSS and kittens

Compass - Sprites

● Configuration options per sprite or sprite-map

● Magic selectors

● Magic imports

● Sprite layouts

● Sass functions

tutorial: http://compass-style.org/help/tutorials/spriting/

Page 24: Drupal 8 themes with SASS, SMACSS and kittens

Compass - SpritesWe need:

● toggle menu icon for mobile

● fixed width and height

● list-items in the menu should

be of equal height

● More control, no extra classes

SCSS Generated CSS

Page 25: Drupal 8 themes with SASS, SMACSS and kittens

Compass - Sprites

That’s extra lines of code, what’s the point?● I am not wasting my time fiddling around with a sprite.

● I don’t care how many pixels a particular image is offset

in the sprite.

● I don’t care what size the image is.

● My designers don’t have to care about creating sprites

or listen to me whine, either.

Page 26: Drupal 8 themes with SASS, SMACSS and kittens

What else can we do with

Compass?

Page 27: Drupal 8 themes with SASS, SMACSS and kittens

Compass - CSS3

Which of these would you rather write?

HINT!

Page 28: Drupal 8 themes with SASS, SMACSS and kittens

Compass - Links

● Hover Link – Underline a link when you hover over it.

● Link Colors – Easy assignment of colors to link states.

● Unstyled Link – Make a link appear like regular text.

Page 29: Drupal 8 themes with SASS, SMACSS and kittens

Compass - Lists

● Bullets – Mixins for managing list bullets.

● Horizontal List – Float a list so it appears horizontally.

● Inline-Block List – set list-elements to inline-block so they appear

horizontally while retaining their structure.

● Inline List – Style a list as inline text.

Page 30: Drupal 8 themes with SASS, SMACSS and kittens

Compass - Text

● Ellipsis – Text truncation with ellipsis.

● Force Wrap – Wrap URLs and long lines of text.

● No Wrap – Remembering whether or not there's a hyphen in white-

space is too hard.

● Text Replacement – Replace text with images.

Page 31: Drupal 8 themes with SASS, SMACSS and kittens

Compass - Typography

Vertical RhythmA massive pile of variables, constants, functions and mixins

to help create and manage vertical rhythm on your site.

This stuff is AWESOME, check it out.

Page 32: Drupal 8 themes with SASS, SMACSS and kittens

Compass also has it’s own

layout import, and there are

many other Sass layout

libraries out there, Susy,

Bootstrap, Zen grids...

Page 33: Drupal 8 themes with SASS, SMACSS and kittens

Compass can be

overkill

Page 34: Drupal 8 themes with SASS, SMACSS and kittens
Page 35: Drupal 8 themes with SASS, SMACSS and kittens

But usually is a timesaver.

● Calculates ems for you with Vertical Rhythm.

● Generates sprites and all kinds of sprite-related

goodies.

● Allows you to do all kinds of things that would normally

require many lines of CSS to be cross-browser

compatible.

● All kinds of fun stuff that is tedious to do by hand.

Page 36: Drupal 8 themes with SASS, SMACSS and kittens

Nesting

is the best thing

ever, right?

.block {h2.title {color: MediumSeaGreen;

}}

Page 37: Drupal 8 themes with SASS, SMACSS and kittens

Only logged in users

actually wanted to see

this colour and only in

the content region...

.logged-in {#content {.block {h2.title {color: MediumSeaGreen;

}}

}}

Page 38: Drupal 8 themes with SASS, SMACSS and kittens

Wait, the

sidebar should

be different!

.logged-in {#content {.block {h2.title {color: MediumSeaGreen;

}}.sidebar-second {.block {h2.title {color: LightGoldenRodYellow;

}}

}}

}

Page 39: Drupal 8 themes with SASS, SMACSS and kittens

Communications

department has a

special announcement,

they want a special

block style to highlight

it.

.logged-in {#content {.block {h2.title {color: MediumSeaGreen;

}}.sidebar-second {.block {h2.title {color: LightGoldenRodYellow;

}&#my-special-block {h2.title {color: YouGottaBeKiddingMe;

}}

}}

}}

Page 40: Drupal 8 themes with SASS, SMACSS and kittens

Poor kitty. :’(.logged-in {#content {.block h2.title {color: MediumSeaGreen;

}.sidebar-second {.block {h2.title {color: LightGoldenRodYellow;

}&#my-special-block {h2.title {color: YouGottaBeKiddingMe;

}}}}}} /* This is called saving space */

.logged-in #content .block h2.title { color: MediumSeaGreen; }

.logged-in #content .sidebar-second .block h2.title { color: LightGoldenRodYellow; }

.logged-in #content .sidebar-second .block#my-special-block h2.title { color: YouGottaBeKiddingMe; }

Page 41: Drupal 8 themes with SASS, SMACSS and kittens

Override this, I dare you.

.node-type-key-topics #main #content .panel-pane.pane-key-topics-portfolio-panel-pane-2 .view-key-topics-portfolio > .view-content li.views-row > div {

}

Real life example. Love the ability to nest selectors in SASS/LESS, but remember that nesting isn’t

always necessary. If you don’t need to nest to achieve a certain level of specificity, don’t do it.

Rule of thumb: Nest no more than 3-4 levels deep!

http://www.creativebloq.com/css3/avoid-css-mistakes-10135080 - For all the insights on everything

everyone is doing wrong.

padding: 0;padding: 0 $base_unit !important;

There, I did it. Neener, neener.

Page 42: Drupal 8 themes with SASS, SMACSS and kittens

Other Pitfalls

● !important

● Over-specificity

● #id selectors - Not cool! Practically

impossible to override and slower

performance.

Please keep your module CSS non-

specific. Overspecify, and I would rather

write my own module than use yours.

Page 43: Drupal 8 themes with SASS, SMACSS and kittens

SMACSS

● Scalable and

● Modular

● Architecture for

● Cascading

● Style

● Sheets

https://smacss.com/

Page 44: Drupal 8 themes with SASS, SMACSS and kittens

Why?

● Organization

● Working with individual, repeatable

components, rather than whole-page

mentality

● Less nesting

● Fewer unnecessary overrides

● BEM - Easier to tell what a component is doing from

name alone

● Styleguide-driven development

Page 45: Drupal 8 themes with SASS, SMACSS and kittens

Sass structure

● Base - Resets, defaults, variables, mixins, no classes or ids!

● Layout - Set it and forget it. No styles!

● Module (Component) - Individual and repeatable separate pieces of your

site (e.g. .button)

o Block

o Element

o Modifier

● State - hover and other contextual issues

● Theme (Skin) - Things specific to your theme, independant of

components, etc.

Page 46: Drupal 8 themes with SASS, SMACSS and kittens

BEM Naming

Conventions

.cat { // Block: A cat. }

.cat__fur { // Element: Fur is an element “in” the cat. }

.cat__box { // Element: Box is a wrapper around the cat. }

.cat--fetish { // Modifier: Fetish is a specific cat. }

.cat--fetish.is-purring { // State: Fetish the cat is purring. }

.living-room .cat { // Theme: A cat in the living room. }

Page 47: Drupal 8 themes with SASS, SMACSS and kittens

Styleguide driven development

Page 48: Drupal 8 themes with SASS, SMACSS and kittens

New to front-end development?

“Here’s a secret: No one

else really knows what

they’re doing either.”

Nicholas Gallagher, 2013

(stolen from John Albin)

Page 49: Drupal 8 themes with SASS, SMACSS and kittens

Further reading/watching/tools

● http://styleguides.io/

● Styleguide driven

development -

https://www.youtube.com/wat

ch?v=0ulC_UiObS0

● http://bourbon.io

Page 50: Drupal 8 themes with SASS, SMACSS and kittens