css3 meets gwt with gss - daniel-kurka.dedaniel-kurka.de/talks/gwtcreate/gss.pdf · • simple and...

47
Developers CSS3 meets GWT with GSS Closure-stylesheets in GWT Daniel Kurka Software Engineer Julien Dramaix Software Engineer

Upload: others

Post on 18-Apr-2020

3 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: CSS3 meets GWT with GSS - daniel-kurka.dedaniel-kurka.de/talks/gwtcreate/gss.pdf · • simple and maintainable CSS with Closure-stylesheets - many exciting features - widely used

Developers

CSS3 meets GWT with GSSClosure-stylesheets in GWT

Daniel Kurka Software Engineer

Julien Dramaix Software Engineer

Page 2: CSS3 meets GWT with GSS - daniel-kurka.dedaniel-kurka.de/talks/gwtcreate/gss.pdf · • simple and maintainable CSS with Closure-stylesheets - many exciting features - widely used

!2

Page 3: CSS3 meets GWT with GSS - daniel-kurka.dedaniel-kurka.de/talks/gwtcreate/gss.pdf · • simple and maintainable CSS with Closure-stylesheets - many exciting features - widely used

!3

Page 4: CSS3 meets GWT with GSS - daniel-kurka.dedaniel-kurka.de/talks/gwtcreate/gss.pdf · • simple and maintainable CSS with Closure-stylesheets - many exciting features - widely used

What is this about?• Introduction to Closure-stylesheets (GSS)

- Variables - Functions - Mixins - Conditionals - Linting - RTL flipping

• Integration with GWT • Timeline • Demo

!4

Page 5: CSS3 meets GWT with GSS - daniel-kurka.dedaniel-kurka.de/talks/gwtcreate/gss.pdf · • simple and maintainable CSS with Closure-stylesheets - many exciting features - widely used

!5http://farm3.staticflickr.com/2663/4241456606_be6fcc8b4e_o.jpg

DRY

Page 6: CSS3 meets GWT with GSS - daniel-kurka.dedaniel-kurka.de/talks/gwtcreate/gss.pdf · • simple and maintainable CSS with Closure-stylesheets - many exciting features - widely used

!6http://upload.wikimedia.org/wikipedia/commons/1/19/Lego_mess.jpg

Page 7: CSS3 meets GWT with GSS - daniel-kurka.dedaniel-kurka.de/talks/gwtcreate/gss.pdf · • simple and maintainable CSS with Closure-stylesheets - many exciting features - widely used

CSS is missing things• variables

- repeating tokens in CSS files • functions

- no way to express derived parameters • macros

- duplicating code common code blocks

!7

Page 8: CSS3 meets GWT with GSS - daniel-kurka.dedaniel-kurka.de/talks/gwtcreate/gss.pdf · • simple and maintainable CSS with Closure-stylesheets - many exciting features - widely used

Closure-stylesheets• are an extension to CSS • write GSS, compile to CSS • adding variables, conditionals, mixing • support minification, linting, RTL flipping, renaming

!8

Page 9: CSS3 meets GWT with GSS - daniel-kurka.dedaniel-kurka.de/talks/gwtcreate/gss.pdf · • simple and maintainable CSS with Closure-stylesheets - many exciting features - widely used

Examples

!9

Page 10: CSS3 meets GWT with GSS - daniel-kurka.dedaniel-kurka.de/talks/gwtcreate/gss.pdf · • simple and maintainable CSS with Closure-stylesheets - many exciting features - widely used

Variables@def BG_COLOR rgb(235, 239, 249);!@def DIALOG_BORDER_COLOR rgb(107, 144, 218);@def DIALOG_BG_COLOR BG_COLOR;!body { background-color: BG_COLOR;}!.dialog { background-color: DIALOG_BG_COLOR; border: 1px solid DIALOG_BORDER_COLOR;}

!10

Page 11: CSS3 meets GWT with GSS - daniel-kurka.dedaniel-kurka.de/talks/gwtcreate/gss.pdf · • simple and maintainable CSS with Closure-stylesheets - many exciting features - widely used

compiles tobody { background-color: #ebeff9;}.dialog { background-color: #ebeff9; border: 1px solid #6b90da;}

!11

Page 12: CSS3 meets GWT with GSS - daniel-kurka.dedaniel-kurka.de/talks/gwtcreate/gss.pdf · • simple and maintainable CSS with Closure-stylesheets - many exciting features - widely used

Functions• Several arithmetic functions

- add() - sub() - mult() - divide() - min() - max()

• Variable number of arguments • Arguments may be purely numeric or need to have the same CSS unit • multiply and divide only allow a CSS unit for the first parameter • not as flexible as CSS calc, but yields more maintainable stylesheets

!12

Page 13: CSS3 meets GWT with GSS - daniel-kurka.dedaniel-kurka.de/talks/gwtcreate/gss.pdf · • simple and maintainable CSS with Closure-stylesheets - many exciting features - widely used

Functions@def LEFT_HAND_NAV_WIDTH 180px;@def LEFT_HAND_NAV_PADDING 3px;!.left_hand_nav { position: absolute; width: LEFT_HAND_NAV_WIDTH; padding: LEFT_HAND_NAV_PADDING;}!.content { position: absolute; margin-left: add(LEFT_HAND_NAV_PADDING, /* padding left */ LEFT_HAND_NAV_WIDTH, LEFT_HAND_NAV_PADDING); /* padding right */}

!13

Page 14: CSS3 meets GWT with GSS - daniel-kurka.dedaniel-kurka.de/talks/gwtcreate/gss.pdf · • simple and maintainable CSS with Closure-stylesheets - many exciting features - widely used

compiles to.left_hand_nav { position: absolute; width: 180px; padding: 3px;}!.content { position: absolute; margin-left: 186px;}

!14

Page 15: CSS3 meets GWT with GSS - daniel-kurka.dedaniel-kurka.de/talks/gwtcreate/gss.pdf · • simple and maintainable CSS with Closure-stylesheets - many exciting features - widely used

Functions• blendColorsHsb(startColor, endColor) blends using HSB values • blendColorsRgb(startColor, endColor) blends using RGB values • makeMutedColor(backgroundColor, foregroundColor [, saturationLoss]) • addHsbToCssColor(baseColor, hueToAdd, saturationToAdd,

brightnessToAdd) • makeContrastingColor(color, similarityIndex) • adjustBrightness(color, brightness) • ….

!15

Page 16: CSS3 meets GWT with GSS - daniel-kurka.dedaniel-kurka.de/talks/gwtcreate/gss.pdf · • simple and maintainable CSS with Closure-stylesheets - many exciting features - widely used

Functions - selectFrom/* Implies MYDEF = FOO ? BAR : BAZ; */@def MYDEF selectFrom(FOO, BAR, BAZ);!/* @def FOO true; to have the effect of @def MYDEF = BAR. */

!16

Page 17: CSS3 meets GWT with GSS - daniel-kurka.dedaniel-kurka.de/talks/gwtcreate/gss.pdf · • simple and maintainable CSS with Closure-stylesheets - many exciting features - widely used

Providing your own functions• Implement GssFunctionMapProvider • pass “--gss-function-map-provider” to the compiler

!17

Page 18: CSS3 meets GWT with GSS - daniel-kurka.dedaniel-kurka.de/talks/gwtcreate/gss.pdf · • simple and maintainable CSS with Closure-stylesheets - many exciting features - widely used

Mixins• reuse a list of parameterized declarations • declaration • a mixing can be used anywhere instead of a declaration

!18

Page 19: CSS3 meets GWT with GSS - daniel-kurka.dedaniel-kurka.de/talks/gwtcreate/gss.pdf · • simple and maintainable CSS with Closure-stylesheets - many exciting features - widely used

Mixins@defmixin size(WIDTH, HEIGHT) { width: WIDTH; height: HEIGHT;}!.logo { @mixin size(150px, 55px); background-image: url('http://www.google.com/images/logo_sm.gif');}

!19

Page 20: CSS3 meets GWT with GSS - daniel-kurka.dedaniel-kurka.de/talks/gwtcreate/gss.pdf · • simple and maintainable CSS with Closure-stylesheets - many exciting features - widely used

compiles to.logo { width: 150px; height: 55px; background-image: url('http://www.google.com/images/logo_sm.gif');}

!20

Page 21: CSS3 meets GWT with GSS - daniel-kurka.dedaniel-kurka.de/talks/gwtcreate/gss.pdf · • simple and maintainable CSS with Closure-stylesheets - many exciting features - widely used

Mixins - for browser specific code@defmixin gradient(POS, HSL1, HSL2, HSL3, COLOR, FALLBACK_COLOR) { background-color: FALLBACK_COLOR; /* fallback color if gradients are not supported */ background-image: -webkit-linear-gradient(POS, hsl(HSL1, HSL2, HSL3), COLOR); /* Chrome 10+,Safari 5.1+ */ /* @alternate */ background-image: -moz-linear-gradient(POS, hsl(HSL1, HSL2, HSL3), COLOR); /* FF3.6+ */ /* @alternate */ background-image: -ms-linear-gradient(POS, hsl(HSL1, HSL2, HSL3), COLOR); /* IE10 */ /* @alternate */ background-image: -o-linear-gradient(POS, hsl(HSL1, HSL2, HSL3), COLOR); /* Opera 11.10+ */}!.header { @mixin gradient(top, 0%, 50%, 70%, #cc0000, #f07575);}

!21

Page 22: CSS3 meets GWT with GSS - daniel-kurka.dedaniel-kurka.de/talks/gwtcreate/gss.pdf · • simple and maintainable CSS with Closure-stylesheets - many exciting features - widely used

Mixins - for browser specific code.header { background-color: #f07575; background-image: -webkit-linear-gradient(top,hsl(0%,50%,70%) ,#cc0000); background-image: -moz-linear-gradient(top,hsl(0%,50%,70%) ,#cc0000); background-image: -ms-linear-gradient(top,hsl(0%,50%,70%) ,#cc0000); background-image: -o-linear-gradient(top,hsl(0%,50%,70%) ,#cc0000);}

!22

Page 23: CSS3 meets GWT with GSS - daniel-kurka.dedaniel-kurka.de/talks/gwtcreate/gss.pdf · • simple and maintainable CSS with Closure-stylesheets - many exciting features - widely used

Conditionals• defining variables in conditionals

!23

Page 24: CSS3 meets GWT with GSS - daniel-kurka.dedaniel-kurka.de/talks/gwtcreate/gss.pdf · • simple and maintainable CSS with Closure-stylesheets - many exciting features - widely used

Conditionals@if (BROWSER_IE) { @if (BROWSER_IE6) { @def GOOG_INLINE_BLOCK_DISPLAY inline; } @elseif (BROWSER_IE7) { @def GOOG_INLINE_BLOCK_DISPLAY inline; } @else { @def GOOG_INLINE_BLOCK_DISPLAY inline-block; }} @elseif (BROWSER_FF2) { @def GOOG_INLINE_BLOCK_DISPLAY -moz-inline-box;} @else { @def GOOG_INLINE_BLOCK_DISPLAY inline-block;}!.goog-inline-block { position: relative; display: GOOG_INLINE_BLOCK_DISPLAY;}

!24

Page 25: CSS3 meets GWT with GSS - daniel-kurka.dedaniel-kurka.de/talks/gwtcreate/gss.pdf · • simple and maintainable CSS with Closure-stylesheets - many exciting features - widely used

Conditionals• “java -jar closure-stylesheets.jar --define BROWSER_FF2 --pretty-print

conditionals-example.gss” .goog-inline-block { position: relative; display: -moz-inline-box; }

• “java -jar closure-stylesheets.jar --pretty-print conditionals-example.gss” .goog-inline-block { position: relative; display: inline-box; }

!25

Page 26: CSS3 meets GWT with GSS - daniel-kurka.dedaniel-kurka.de/talks/gwtcreate/gss.pdf · • simple and maintainable CSS with Closure-stylesheets - many exciting features - widely used

Linting• check your CSS for errors • will fail the compile with an error message

!26

Page 27: CSS3 meets GWT with GSS - daniel-kurka.dedaniel-kurka.de/talks/gwtcreate/gss.pdf · • simple and maintainable CSS with Closure-stylesheets - many exciting features - widely used

Linting.logo { width: 150px; height: 55px; background-image: urel('http://www.google.com/images/logo_sm.gif'); border-color: #DCDCDC; border-color: rgba(0, 0, 0, 0.1);}

!27

Page 28: CSS3 meets GWT with GSS - daniel-kurka.dedaniel-kurka.de/talks/gwtcreate/gss.pdf · • simple and maintainable CSS with Closure-stylesheets - many exciting features - widely used

Linting.logo { width: 150px; height: 55px; background-image: urel('http://www.google.com/images/logo_sm.gif'); border-color: #DCDCDC; border-color: rgba(0, 0, 0, 0.1);}!Unknown function "urel" in linting-example.gss at line 4 column 21: background-image: urel('http://www.google.com/images/logo_sm.gif'); ^Detected multiple identical, non-alternate declarations in the same ruleset.If this is intentional please use the /* @alternate */ annotation.border-color:[rgba(0,0,0,0.1)] in linting-example.gss at line 7 column 1:}^2 error(s)

!28

Page 29: CSS3 meets GWT with GSS - daniel-kurka.dedaniel-kurka.de/talks/gwtcreate/gss.pdf · • simple and maintainable CSS with Closure-stylesheets - many exciting features - widely used

RTL flipping.logo { margin-left: 10px;}!.shortcut_accelerator { /* Keyboard shortcuts are untranslated; always left-to-right. */ /* @noflip */ direction: ltr; border-right: 2px solid #ccc; padding: 0 2px 0 4px;}

!29

Page 30: CSS3 meets GWT with GSS - daniel-kurka.dedaniel-kurka.de/talks/gwtcreate/gss.pdf · • simple and maintainable CSS with Closure-stylesheets - many exciting features - widely used

RTL flipping.logo { margin-right: 10px;}!.shortcut_accelerator { direction: ltr; border-left: 2px solid #ccc; padding: 0 4px 0 2px;}

!30

Page 31: CSS3 meets GWT with GSS - daniel-kurka.dedaniel-kurka.de/talks/gwtcreate/gss.pdf · • simple and maintainable CSS with Closure-stylesheets - many exciting features - widely used

GSS

!31

Page 32: CSS3 meets GWT with GSS - daniel-kurka.dedaniel-kurka.de/talks/gwtcreate/gss.pdf · • simple and maintainable CSS with Closure-stylesheets - many exciting features - widely used

Integration with GWT• Why ?

- Closure-stylesheets are powerful - easier to maintain CSS - GWT needs better CSS3 support

• What does it look like? - A new GSSResource - similar to CSSResource

!32

Page 33: CSS3 meets GWT with GSS - daniel-kurka.dedaniel-kurka.de/talks/gwtcreate/gss.pdf · • simple and maintainable CSS with Closure-stylesheets - many exciting features - widely used

CSS: application.gss@def PADDING_RIGHT 50px;@def PADDING_LEFT 50px;!@defmixin size(WIDTH, HEIGHT) { width: WIDTH; height: HEIGHT;}!.header, span[data-role^="header"] { display: block; @mixin size(150px, 50px);}!.header-with-padding { @mixin size(add(PADDING_RIGHT, 150px, PADDING_LEFT), 50px); padding-right: PADDING_RIGHT;}

!33

Page 34: CSS3 meets GWT with GSS - daniel-kurka.dedaniel-kurka.de/talks/gwtcreate/gss.pdf · • simple and maintainable CSS with Closure-stylesheets - many exciting features - widely used

Java: define GssResourcepublic interface ApplicationResources extends ClientBundle {! public interface ApplicationGss extends GssResource { int paddingRight(); String header();! @ClassName("header-with-padding") String headerWithPadding(); }! @Source("application.gss") public ApplicationGss style();}

!34

Page 35: CSS3 meets GWT with GSS - daniel-kurka.dedaniel-kurka.de/talks/gwtcreate/gss.pdf · • simple and maintainable CSS with Closure-stylesheets - many exciting features - widely used

Usage:ApplicationResources resources = GWT.create(ApplicationResources.class);ApplicationGss style = resources.style();style.ensureInjected();!// ...myWidget.addStyleName(style.header());

!35

Page 36: CSS3 meets GWT with GSS - daniel-kurka.dedaniel-kurka.de/talks/gwtcreate/gss.pdf · • simple and maintainable CSS with Closure-stylesheets - many exciting features - widely used

CssResource features• @sprite • @eval • value function

!36

Page 37: CSS3 meets GWT with GSS - daniel-kurka.dedaniel-kurka.de/talks/gwtcreate/gss.pdf · • simple and maintainable CSS with Closure-stylesheets - many exciting features - widely used

Image sprite (@sprite).addIcon { gwt-sprite: 'addIcon';}

!37

Page 38: CSS3 meets GWT with GSS - daniel-kurka.dedaniel-kurka.de/talks/gwtcreate/gss.pdf · • simple and maintainable CSS with Closure-stylesheets - many exciting features - widely used

Runtime substitution (@eval)@def BLACK eval(‘mypackage.GssSampleApplication.black()’);!.black { color: eval('mypackage.GssSampleApplication.black()');}

!38

Page 39: CSS3 meets GWT with GSS - daniel-kurka.dedaniel-kurka.de/talks/gwtcreate/gss.pdf · • simple and maintainable CSS with Closure-stylesheets - many exciting features - widely used

Value function@def ICON_WIDTH value('addIcon.getWidth', 'px');

!39

Page 40: CSS3 meets GWT with GSS - daniel-kurka.dedaniel-kurka.de/talks/gwtcreate/gss.pdf · • simple and maintainable CSS with Closure-stylesheets - many exciting features - widely used

How far is this done?• supports all Closure-stylesheet features • supported CssResource features

- image sprites - runtime substitution - value function - RTL support

!40

Page 41: CSS3 meets GWT with GSS - daniel-kurka.dedaniel-kurka.de/talks/gwtcreate/gss.pdf · • simple and maintainable CSS with Closure-stylesheets - many exciting features - widely used

What is missing?• conditionals based on permutations !@if user.agent safari { /* ... */ }!@if locale en { /* ... */ }

!41

Page 42: CSS3 meets GWT with GSS - daniel-kurka.dedaniel-kurka.de/talks/gwtcreate/gss.pdf · • simple and maintainable CSS with Closure-stylesheets - many exciting features - widely used

Roadmap• Ready to use, but there might still be issues • please test & report issues at: https://github.com/jDramaix/gss.gwt/

issues • Expect missing user agent conditionals in Q1 • Unclear: How will CssResource transition look like • If everything goes smoothly, expect GssResource in GWT 3.0, but you can

use it right now!

!42

Page 43: CSS3 meets GWT with GSS - daniel-kurka.dedaniel-kurka.de/talks/gwtcreate/gss.pdf · • simple and maintainable CSS with Closure-stylesheets - many exciting features - widely used

Demo

!43

Page 44: CSS3 meets GWT with GSS - daniel-kurka.dedaniel-kurka.de/talks/gwtcreate/gss.pdf · • simple and maintainable CSS with Closure-stylesheets - many exciting features - widely used

Summary• simple and maintainable CSS with Closure-stylesheets

- many exciting features - widely used inside of Google

• GssResource - brings Closure-stylesheets to GWT, aka CssResource for CSS - brings all CSS3 features to GWT - thanks to Julien Dramaix for this big contribution

• Roadmap - Test and report bugs!

!44

Page 45: CSS3 meets GWT with GSS - daniel-kurka.dedaniel-kurka.de/talks/gwtcreate/gss.pdf · • simple and maintainable CSS with Closure-stylesheets - many exciting features - widely used

Questions?

!45

Page 46: CSS3 meets GWT with GSS - daniel-kurka.dedaniel-kurka.de/talks/gwtcreate/gss.pdf · • simple and maintainable CSS with Closure-stylesheets - many exciting features - widely used

Julien Dramaixgplus: https://plus.google.com/+JulienDramaix twitter: @jDramaix

Page 47: CSS3 meets GWT with GSS - daniel-kurka.dedaniel-kurka.de/talks/gwtcreate/gss.pdf · • simple and maintainable CSS with Closure-stylesheets - many exciting features - widely used

Daniel Kurkawww.m-gwt.com !gplus: daniel-kurka.de/+ !twitter: @dankurka