Slide 1

Slide 1 text

4½ Methods for Theming in (S)CSS Harry Roberts – CSSConf Australia – March, 2015

Slide 2

Slide 2 text

Hello, Australia!

Slide 3

Slide 3 text

Hello, Australia! Harry Roberts Consultant Front-end Architect @csswizardry #4halfCSS

Slide 4

Slide 4 text

Consultant Front-end Architect I help clients solve UI problems. Lots and lots of different clients. Lots and lots of different problems.

Slide 5

Slide 5 text

Theming Is Common

Slide 6

Slide 6 text

Theming Is Common SaaS clients want to offer a varied product range. White-label clients want to offer bespoke themes. Hub clients want each part of a product to have its own colours. Social network clients want to allow users to personalise things.

Slide 7

Slide 7 text

Theming vs. Customisation

Slide 8

Slide 8 text

Theming: defined by developers.

Slide 9

Slide 9 text

Theming Requires developer input. Finite amount of variants. Known rules and constraints.

Slide 10

Slide 10 text

No content

Slide 11

Slide 11 text

Customisation: defined by users.

Slide 12

Slide 12 text

Customisation Requires no developer input. Infinite possibilities. Whatever the user says goes.

Slide 13

Slide 13 text

No content

Slide 14

Slide 14 text

Advice on Theming

Slide 15

Slide 15 text

Advice on Theming Avoid it if possible—make sure there’s a real business case. KISS—always reduce the complexity involved. Only make changes to cosmetic aspects—avoid altering box-model. Enforce rules—deviation is expensive. Use it as an up-sell—‘Any deviation is going to cost you.’

Slide 16

Slide 16 text

A Quick Note on Preprocessors

Slide 17

Slide 17 text

A Quick Note on Preprocessors Some don’t need a preprocessor. Some don’t neeeeeed a preprocessor. Some need a preprocessor.

Slide 18

Slide 18 text

You should probably be using
 a preprocessor.

Slide 19

Slide 19 text

No content

Slide 20

Slide 20 text

#1 – Theme Layer

Slide 21

Slide 21 text

Overriding default style with additional CSS.

Slide 22

Slide 22 text

Theme Layer Possibly the most common approach (currently). Start off with the base/default styling. Use additional, subsequent CSS to override and redefine.

Slide 23

Slide 23 text

Suited To Bespoke theming for clients.

Slide 24

Slide 24 text

@import "components.tabs"; ... @import "theme.tabs";

Slide 25

Slide 25 text

@import "components.tabs"; @import "components.buttons"; ... @import "theme.tabs"; @import "theme.buttons";

Slide 26

Slide 26 text

// _components.tabs.scss .tabs { margin: 0; padding: 0; background-color: gray; } ... // _theme.tabs.scss .tabs { background-color: red;
 }

Slide 27

Slide 27 text

// _components.tabs.scss .tabs { margin: 0; padding: 0; background-color: gray; } ... // _theme.tabs.scss .tabs { background-color: red;
 }

Slide 28

Slide 28 text

The Good You can swap layers out. You can theme anything.

Slide 29

Slide 29 text

The Bad Lots of redundancy. Wasted CSS over the wire. Shards styles across multiple files.

Slide 30

Slide 30 text

#2 – Stateful Theming

Slide 31

Slide 31 text

Styling a UI based on a state or condition.

Slide 32

Slide 32 text

Stateful Theming Different skins based on conditions. Allow themes to be switched on the client.

Slide 33

Slide 33 text

Suited To Style switchers. Individually styled sections or areas of a site.

Slide 34

Slide 34 text

No content

Slide 35

Slide 35 text

No content

Slide 36

Slide 36 text

.tabs { background-color: gray; .t-red & { background-color: red; } .t-blue & { background-color: blue; } }

Slide 37

Slide 37 text

.tabs { background-color: gray; .t-red & { background-color: red; } .t-blue & { background-color: blue; } }

Slide 38

Slide 38 text

’The tabs when in context.’

Slide 39

Slide 39 text

.tabs { background-color: gray; } .t-red .tabs { background-color: red; } .t-blue .tabs { background-color: blue; }

Slide 40

Slide 40 text

Now we just turn that switch on…

Slide 41

Slide 41 text

    ...

Slide 42

Slide 42 text

    ...

Slide 43

Slide 43 text

    ...

Slide 44

Slide 44 text

Stateful Utility Classes

Slide 45

Slide 45 text

.u-color-current { .t-red & { color: red; } .t-blue & { color: blue; } }

Slide 46

Slide 46 text

.u-color-current { .t-red & { color: red; } .t-blue & { color: blue; } }

Slide 47

Slide 47 text

.t-red .u-color-current { color: red; } .t-blue .u-color-current { color: blue; }

Slide 48

Slide 48 text

A utility class that tracks the current theme.

Slide 49

Slide 49 text

...

Slide 50

Slide 50 text

...

Slide 51

Slide 51 text

...

Slide 52

Slide 52 text

The Good Bundle a number of themes into one codebase. Great for style-switchers. Perfect for ‘areas’ of a site. You can theme anything.

Slide 53

Slide 53 text

The Bad Bundle a number of themes into one codebase. Potential redundancy.

Slide 54

Slide 54 text

#3 – Config Theming

Slide 55

Slide 55 text

Invoking a theme based on settings.

Slide 56

Slide 56 text

Config Theming Switching between themes dev-side. Compiling the same CSS with a different skin based on a setting.

Slide 57

Slide 57 text

Suited To Spitting out differently themed variants of a UI. Offering off-the-shelf themes to clients.

Slide 58

Slide 58 text

@import "settings.config"; ... @import "components.tabs";

Slide 59

Slide 59 text

// _settings.config.scss $theme: red; // _components.tabs.scss .tabs { margin: 0; padding: 0; 
 @if ($theme == red) { background-color: red; } @else { background-color: gray; } }

Slide 60

Slide 60 text

// _settings.config.scss $config: ( theme: red, env: dev, ); // Get config out of our map. @function config($key) { @return map-get($config, $key); }

Slide 61

Slide 61 text

// _components.tabs.scss .tabs { margin: 0; padding: 0; 
 @if (config(theme) == red) { background-color: red; } @else { background-color: gray; } }

Slide 62

Slide 62 text

The Good Only send as little CSS over the wire as necessary. Alter the entire theme from one location. You can theme anything.

Slide 63

Slide 63 text

The Bad Lots of logic in your Sass. Only supports a finite number of themes. Theme information is spread out through the codebase. Adding themes could be laborious.

Slide 64

Slide 64 text

#4 – Theme Palettes

Slide 65

Slide 65 text

Holding entire themes in a palette file.

Slide 66

Slide 66 text

Theme Palettes Preload all of your theme settings into a palette. Inject these settings into the project later.

Slide 67

Slide 67 text

Suited To Fully bespoke theming for clients. Adding specific branding to a project.

Slide 68

Slide 68 text

@import "settings.palette"; ... @import "components.tabs";

Slide 69

Slide 69 text

// _settings.palette.scss $color-red: red; ... $color-tabs-background: $color-red;
 ... // _components.tabs.scss .tabs { margin: 0; padding: 0; background-color: $color-tabs-background; }

Slide 70

Slide 70 text

// _settings.palette.scss $color-brand: #BADA55; $color-brand-highlight: lighten($color-brand, 10%); $color-red: red; $color-green: green; $color-blue: blue; ... $color-links: $color-brand; $color-links-hover: $color-brand-highilght; $color-tabs-background: $color-red; $color-btn-background: $color-blue;

Slide 71

Slide 71 text

The Good Zero redundancy. Perfect for bespoke theming—just plug in the client’s brand colours. Completely restyle the project from one file.

Slide 72

Slide 72 text

The Bad Limited mainly just to theming colours.

Slide 73

Slide 73 text

#5 – User Customisation

Slide 74

Slide 74 text

Letting users style their own UIs.

Slide 75

Slide 75 text

User Customisation Letting users decide their own colours. Done through a CMS/admin interface.

Slide 76

Slide 76 text

Suited To Social networks. SaaS platforms. Brandable software products.

Slide 77

Slide 77 text

.u-user-color { color: red; } .u-user-color-background { background-color: red; } ...
    ...

Slide 78

Slide 78 text

Just dump it all in the view.

Slide 79

Slide 79 text

User Customisation Drop the hex value(s) into a style block. Bind onto utility classes rather than existing hooks. Now just a case of decorating in the UI with the classes.

Slide 80

Slide 80 text

.u-user-color { color: red; } .u-user-color-background { background-color: red; } ...
    ...

Slide 81

Slide 81 text

No content

Slide 82

Slide 82 text

@necolas

Slide 83

Slide 83 text

No content

Slide 84

Slide 84 text

No content

Slide 85

Slide 85 text

The Good Requires zero developer input. Allows users to ‘own’ their profile/platform. Incredibly pragmatic—always a good thing!

Slide 86

Slide 86 text

The Bad Requires zero developer input. Lots of redundancy. Wasted CSS over the wire. Lose the ability to cache styles.

Slide 87

Slide 87 text

When to Use Which?

Slide 88

Slide 88 text

When to Use Which? Who’s doing the styling: you or the user? – User Customisation. Do the themes need to change once they’re on the client? – Stateful or User Customisation. Do you have themes that you want people to be able to toggle? – Stateful. Do you have sections of the site that need to look different? – Stateful. Do you have preset themes that a client can choose from? – Config. Do you offer bespoke theming for white-label clients? – Theme Layer or Theme Palette.

Slide 89

Slide 89 text

Thank You!

Slide 90

Slide 90 text

Thank You! Follow me: @csswizardry Hire me: csswizardry.com/work Slides: speakerdeck.com/csswizardry Code: csswizardry.net/talks/2015/theming.zip