SlideShare una empresa de Scribd logo
1 de 38
Descargar para leer sin conexión
Styling 
Components 
with JavaScript 
@bensmithett
Warning 
» Not a tutorial for use in production! 
» I'm not even using any of this outside 
late night hacks 
But there are some interesting new ideas. 
Let's explore them & challenge CSS best 
practices!
Components are awesome! 
Nobody builds pages any more. 
Here's an example Profile component: 
components/ 
Profile/ 
index.hbs 
index.css 
index.js
HTML Template 
<div class="profile"> 
<img class="profile__avatar" src="{{avatarUrl}}.jpg" /> 
<strong>{{username}}</strong> 
</div> 
Style 
.profile { 
border: 1px solid #ddd; 
overflow: hidden; 
} 
.profile__avatar { 
float: left; 
margin-right: 10px; 
}
Behaviour 
var Profile = function (el) { 
el.addEventListener("click", function () { 
console.log("hai!"); 
}); 
this.el = el; 
this.tmpl = Handlebars.compile(someTemplateString); 
}; 
Profile.prototype.render = function (state) { 
this.el.innerHTML = this.tmpl(state); 
};
React combines HTML structure & behaviour 
var React = require("react"); 
var Profile = React.createClass({ 
handleClick: function () { 
console.log("hai"); 
}, 
render: function () { 
return ( 
<div class="profile"> 
<img class="profile__avatar" src="{this.props.avatarUrl}.jpg" 
onClick={this.handleClick} /> 
<strong>{this.props.username}</strong> 
</div> 
); 
} 
}); 
module.exports = Profile;
That's a big dirty lie 
The component's CSS is one of its 
concerns, but it's off in some random 
other file. 
components/ 
Profile/ 
index.css 
index.jsx
The only connection: a class name 
// JS 
render: function () { 
return ( 
<div class="profile"> 
// ... 
</div> 
) 
} 
/* CSS */ 
.profile 
border: 1px solid #ddd; 
overflow: hidden;
Most things 
» JS Dependencies are explicitly required 
» HTML structure is right there in the 
file 
» JS behaviour is right there in the file 
CSS 
» In another file, the classes might have 
the same name ¯_()_/¯ 
It's a crappy, vague connection.
CSS builds are a bit backwards 
//app.scss 
@import vendor/Normalize.css; 
@import base; 
@import components/Header; 
@import components/Profile; 
@import components/Footer; 
You need to know which bits of CSS your 
app requires. Lame.
What if our JS build automatically 
created a stylesheet based only on the 
components we use? 
// app.js 
var Profile = require(./components/Profile); 
var Header = require(./components/Header); 
var Footer = require(./components/Footer); 
// app.css 
// Somehow... 
// components/Profile/index.css 
// components/Header/index.css 
// components/Footer/index.css 
// ... end up here?
http://webpack.github.io/
var React = require(react); 
require(./index.css); 
var Profile = React.createClass({ 
render: function () { 
return ( 
div class=profile / 
); 
} 
}); 
module.exports = Profile;
// app.js 
var Profile = require(./components/Profile); 
var Header = require(./components/Header); 
var Footer = require(./components/Footer); 
// app.css generated by webpack 
.profile { ... } 
.profile__avatar { ... } 
.header { ... } 
.footer { ... }
Hooray! 
CSS is just another dependency we can 
require() in our component
Hooray? 
components/ 
Profile/ 
index.css 
index.jsx 
» Still working across 2 files 
» Need to maintain class names across 2 
files 
... still a bit lame.
React can do inline styles 
// Profile/index.js 
var Profile = React.createClass({ 
styles: { 
border: 1px solid #ddd, 
overflow: hidden 
}, 
render: function () { 
return( 
div style={this.styles} / 
); 
} 
}); 
!-- DOM generated by React -- 
div style=border: 1px solid #ddd; overflow: hidden; 
/div
Nobody likes inline 
styles though
What we really want: 
» Declare styles in the component, like 
we do with inline styles 
» Build process that... 
» converts them to a CSS class 
» spits out a shiny, auto-generated 
app.css 
» component knows to use that class 
name
React-style does that! 
» https://github.com/SanderSpies/react-style 
» http://andreypopp.com/posts/2014-08-06- 
react-style.html 
(with a little help from webpack)
var React = require(react/addons); 
var ReactStyle = require(react-style); 
var Profile = React.createClass({ 
styles: ReactStyle(function () { 
return { 
backgroundColor: green, 
border: 1px solid #ddd 
}; 
}), 
render: function () { 
return( 
div styles={this.styles()} / 
); 
} 
}); 
module.exports = Profile;
!-- DOM generated by React -- 
div class=a 
... 
/div 
// app.css generated by React-style  Webpack 
.a { 
background-color: green; 
border: 1px solid #ddd; 
}
Demo 
Compiling with default compressed class 
names
Demo 
Formatting class names
Do you even need 
a CSS naming 
convention?
not really... 
» Styles are tightly coupled part of the 
component, not a separate thing 
» CSS class naming conventions are a 
project setting, not an inherent 
property of the component 
» Dev: BEM class names for easy 
debugging 
» Prod: Tiny compressed class names
I 3 Sass 
$red: #f00; 
$grid-columns: 12; 
$base-font-size: 16px; 
@function px-to-em($px, $base: $base-font-size) { 
@return ($px / $base) * 1em; 
} 
%placeholder { 
color: $red; 
} 
.thing { 
@extend %placeholder; 
padding: 10px; 
}
What is a Preprocessor? 
A language that helps us generate blocks 
of key:value pairs. 
selector { 
property: value; 
other-property: other-value; 
}
What is a Preprocessor? 
A language that helps us generate blocks 
of key:value pairs. 
selector = { 
property: value, 
other-property: other-value 
}; 
JavaScript can do that!
JS already has lots of Real Programming 
Language Things TM 
» Variables 
» Functions 
» Arrays  Objects 
» Loops 
» Maths 
» String manipulation 
» Dependency management
Warning! 
Total pseudocode, nothing past this point 
actually works
Example: color variables 
var colors = require(./color_palette); 
var Profile = React.createClass({ 
styles: ReactStyle(function () { 
return { 
color: colors[hotPink], 
}; 
}), 
render: function () { 
return( 
div styles={this.styles()} / 
); 
} 
});
Example: Generate a grid 
var gridColumns = 12; 
var styles = {}; 
for (var i = 1; i = gridColumns; i++) { 
var width = (i / gridColumns) * 100; 
styles[span- + i] = ReactStyle(function () { 
return { 
float: left, 
width: width + % 
} 
}); 
} 
var GridColumn = React.createClass({ 
styles: styles, 
render: function () { 
var columns = span- + this.props.columns; 
return( 
div styles={this.styles[columns]()} / 
); 
} 
});
2015 Hipster Preprocessor 
JavaScript?!
The end :) 
@bensmithett 
https://github.com/bensmithett/react-style-example 
https://github.com/SanderSpies/react-style 
https://github.com/chenglou/rcss 
https://github.com/hedgerwang/react-styles 
https://github.com/elierotenberg/react-css

Más contenido relacionado

La actualidad más candente

SharePoint and jQuery Essentials
SharePoint and jQuery EssentialsSharePoint and jQuery Essentials
SharePoint and jQuery EssentialsMark Rackley
 
JavaScript and jQuery Basics
JavaScript and jQuery BasicsJavaScript and jQuery Basics
JavaScript and jQuery BasicsKaloyan Kosev
 
Real World Web components
Real World Web componentsReal World Web components
Real World Web componentsJarrod Overson
 
Cartegraph Live HTML, CSS, JavaScript and jQuery Training
Cartegraph Live HTML, CSS, JavaScript and jQuery TrainingCartegraph Live HTML, CSS, JavaScript and jQuery Training
Cartegraph Live HTML, CSS, JavaScript and jQuery TrainingShane Church
 
Introduction to javascript templating using handlebars.js
Introduction to javascript templating using handlebars.jsIntroduction to javascript templating using handlebars.js
Introduction to javascript templating using handlebars.jsMindfire Solutions
 
Oracle Application Express & jQuery Mobile - OGh Apex Dag 2012
Oracle Application Express & jQuery Mobile - OGh Apex Dag 2012Oracle Application Express & jQuery Mobile - OGh Apex Dag 2012
Oracle Application Express & jQuery Mobile - OGh Apex Dag 2012crokitta
 
Building Web Sites that Work Everywhere
Building Web Sites that Work EverywhereBuilding Web Sites that Work Everywhere
Building Web Sites that Work EverywhereDoris Chen
 
Internet and Web Technology (CLASS-10) [Node.js] | NIC/NIELIT Web Technology
Internet and Web Technology (CLASS-10) [Node.js] | NIC/NIELIT Web Technology Internet and Web Technology (CLASS-10) [Node.js] | NIC/NIELIT Web Technology
Internet and Web Technology (CLASS-10) [Node.js] | NIC/NIELIT Web Technology Ayes Chinmay
 
How I Learned to Stop Worrying and Love jQuery (Jan 2013)
How I Learned to Stop Worrying and Love jQuery (Jan 2013)How I Learned to Stop Worrying and Love jQuery (Jan 2013)
How I Learned to Stop Worrying and Love jQuery (Jan 2013)David Giard
 
Short intro to JQuery and Modernizr
Short intro to JQuery and ModernizrShort intro to JQuery and Modernizr
Short intro to JQuery and ModernizrJussi Pohjolainen
 
jQuery in the [Aol.] Enterprise
jQuery in the [Aol.] EnterprisejQuery in the [Aol.] Enterprise
jQuery in the [Aol.] EnterpriseDave Artz
 
Managing JavaScript Dependencies With RequireJS
Managing JavaScript Dependencies With RequireJSManaging JavaScript Dependencies With RequireJS
Managing JavaScript Dependencies With RequireJSDen Odell
 
jQuery Tips Tricks Trivia
jQuery Tips Tricks TriviajQuery Tips Tricks Trivia
jQuery Tips Tricks TriviaCognizant
 
JsViews - Next Generation jQuery Templates
JsViews - Next Generation jQuery TemplatesJsViews - Next Generation jQuery Templates
JsViews - Next Generation jQuery TemplatesBorisMoore
 

La actualidad más candente (19)

RequireJS & Handlebars
RequireJS & HandlebarsRequireJS & Handlebars
RequireJS & Handlebars
 
SharePoint and jQuery Essentials
SharePoint and jQuery EssentialsSharePoint and jQuery Essentials
SharePoint and jQuery Essentials
 
JavaScript and jQuery Basics
JavaScript and jQuery BasicsJavaScript and jQuery Basics
JavaScript and jQuery Basics
 
Real World Web components
Real World Web componentsReal World Web components
Real World Web components
 
Cartegraph Live HTML, CSS, JavaScript and jQuery Training
Cartegraph Live HTML, CSS, JavaScript and jQuery TrainingCartegraph Live HTML, CSS, JavaScript and jQuery Training
Cartegraph Live HTML, CSS, JavaScript and jQuery Training
 
Introduction to javascript templating using handlebars.js
Introduction to javascript templating using handlebars.jsIntroduction to javascript templating using handlebars.js
Introduction to javascript templating using handlebars.js
 
Oracle Application Express & jQuery Mobile - OGh Apex Dag 2012
Oracle Application Express & jQuery Mobile - OGh Apex Dag 2012Oracle Application Express & jQuery Mobile - OGh Apex Dag 2012
Oracle Application Express & jQuery Mobile - OGh Apex Dag 2012
 
Building Web Sites that Work Everywhere
Building Web Sites that Work EverywhereBuilding Web Sites that Work Everywhere
Building Web Sites that Work Everywhere
 
Internet and Web Technology (CLASS-10) [Node.js] | NIC/NIELIT Web Technology
Internet and Web Technology (CLASS-10) [Node.js] | NIC/NIELIT Web Technology Internet and Web Technology (CLASS-10) [Node.js] | NIC/NIELIT Web Technology
Internet and Web Technology (CLASS-10) [Node.js] | NIC/NIELIT Web Technology
 
J query
J queryJ query
J query
 
How I Learned to Stop Worrying and Love jQuery (Jan 2013)
How I Learned to Stop Worrying and Love jQuery (Jan 2013)How I Learned to Stop Worrying and Love jQuery (Jan 2013)
How I Learned to Stop Worrying and Love jQuery (Jan 2013)
 
Short intro to JQuery and Modernizr
Short intro to JQuery and ModernizrShort intro to JQuery and Modernizr
Short intro to JQuery and Modernizr
 
JQuery
JQueryJQuery
JQuery
 
jQuery in the [Aol.] Enterprise
jQuery in the [Aol.] EnterprisejQuery in the [Aol.] Enterprise
jQuery in the [Aol.] Enterprise
 
DirectToWeb 2.0
DirectToWeb 2.0DirectToWeb 2.0
DirectToWeb 2.0
 
Managing JavaScript Dependencies With RequireJS
Managing JavaScript Dependencies With RequireJSManaging JavaScript Dependencies With RequireJS
Managing JavaScript Dependencies With RequireJS
 
jQuery Tips Tricks Trivia
jQuery Tips Tricks TriviajQuery Tips Tricks Trivia
jQuery Tips Tricks Trivia
 
JQuery
JQueryJQuery
JQuery
 
JsViews - Next Generation jQuery Templates
JsViews - Next Generation jQuery TemplatesJsViews - Next Generation jQuery Templates
JsViews - Next Generation jQuery Templates
 

Destacado

Modern networking for php developers - Dutch PHP conference 2015
Modern networking for php developers - Dutch PHP conference 2015Modern networking for php developers - Dutch PHP conference 2015
Modern networking for php developers - Dutch PHP conference 2015SynchroM
 
UX, ethnography and possibilities: for Libraries, Museums and Archives
UX, ethnography and possibilities: for Libraries, Museums and ArchivesUX, ethnography and possibilities: for Libraries, Museums and Archives
UX, ethnography and possibilities: for Libraries, Museums and ArchivesNed Potter
 
Designing Teams for Emerging Challenges
Designing Teams for Emerging ChallengesDesigning Teams for Emerging Challenges
Designing Teams for Emerging ChallengesAaron Irizarry
 
Visual Design with Data
Visual Design with DataVisual Design with Data
Visual Design with DataSeth Familian
 
3 Things Every Sales Team Needs to Be Thinking About in 2017
3 Things Every Sales Team Needs to Be Thinking About in 20173 Things Every Sales Team Needs to Be Thinking About in 2017
3 Things Every Sales Team Needs to Be Thinking About in 2017Drift
 
How to Become a Thought Leader in Your Niche
How to Become a Thought Leader in Your NicheHow to Become a Thought Leader in Your Niche
How to Become a Thought Leader in Your NicheLeslie Samuel
 

Destacado (6)

Modern networking for php developers - Dutch PHP conference 2015
Modern networking for php developers - Dutch PHP conference 2015Modern networking for php developers - Dutch PHP conference 2015
Modern networking for php developers - Dutch PHP conference 2015
 
UX, ethnography and possibilities: for Libraries, Museums and Archives
UX, ethnography and possibilities: for Libraries, Museums and ArchivesUX, ethnography and possibilities: for Libraries, Museums and Archives
UX, ethnography and possibilities: for Libraries, Museums and Archives
 
Designing Teams for Emerging Challenges
Designing Teams for Emerging ChallengesDesigning Teams for Emerging Challenges
Designing Teams for Emerging Challenges
 
Visual Design with Data
Visual Design with DataVisual Design with Data
Visual Design with Data
 
3 Things Every Sales Team Needs to Be Thinking About in 2017
3 Things Every Sales Team Needs to Be Thinking About in 20173 Things Every Sales Team Needs to Be Thinking About in 2017
3 Things Every Sales Team Needs to Be Thinking About in 2017
 
How to Become a Thought Leader in Your Niche
How to Become a Thought Leader in Your NicheHow to Become a Thought Leader in Your Niche
How to Become a Thought Leader in Your Niche
 

Similar a Styling components with JavaScript

Practical HTML5: Using It Today
Practical HTML5: Using It TodayPractical HTML5: Using It Today
Practical HTML5: Using It TodayDoris Chen
 
Javascript first-class citizenery
Javascript first-class citizeneryJavascript first-class citizenery
Javascript first-class citizenerytoddbr
 
[Coscup 2012] JavascriptMVC
[Coscup 2012] JavascriptMVC[Coscup 2012] JavascriptMVC
[Coscup 2012] JavascriptMVCAlive Kuo
 
Guia de Sobrevivência JS no mundo Open Source
Guia de Sobrevivência JS no mundo Open SourceGuia de Sobrevivência JS no mundo Open Source
Guia de Sobrevivência JS no mundo Open SourceLeonardo Balter
 
Web performance essentials - Goodies
Web performance essentials - GoodiesWeb performance essentials - Goodies
Web performance essentials - GoodiesJerry Emmanuel
 
Web Development for UX Designers
Web Development for UX DesignersWeb Development for UX Designers
Web Development for UX DesignersAshlimarie
 
Jquery presentation
Jquery presentationJquery presentation
Jquery presentationMevin Mohan
 
Jarv.us Showcase — SenchaCon 2011
Jarv.us Showcase — SenchaCon 2011Jarv.us Showcase — SenchaCon 2011
Jarv.us Showcase — SenchaCon 2011Chris Alfano
 
J query b_dotnet_ug_meet_12_may_2012
J query b_dotnet_ug_meet_12_may_2012J query b_dotnet_ug_meet_12_may_2012
J query b_dotnet_ug_meet_12_may_2012ghnash
 
TDC2017 | Florianopolis - Trilha DevOps How we figured out we had a SRE team ...
TDC2017 | Florianopolis - Trilha DevOps How we figured out we had a SRE team ...TDC2017 | Florianopolis - Trilha DevOps How we figured out we had a SRE team ...
TDC2017 | Florianopolis - Trilha DevOps How we figured out we had a SRE team ...tdc-globalcode
 
Django Rest Framework and React and Redux, Oh My!
Django Rest Framework and React and Redux, Oh My!Django Rest Framework and React and Redux, Oh My!
Django Rest Framework and React and Redux, Oh My!Eric Palakovich Carr
 
Integrating React.js Into a PHP Application
Integrating React.js Into a PHP ApplicationIntegrating React.js Into a PHP Application
Integrating React.js Into a PHP ApplicationAndrew Rota
 
Javascript unit testing, yes we can e big
Javascript unit testing, yes we can   e bigJavascript unit testing, yes we can   e big
Javascript unit testing, yes we can e bigAndy Peterson
 
jQuery & 10,000 Global Functions: Working with Legacy JavaScript
jQuery & 10,000 Global Functions: Working with Legacy JavaScriptjQuery & 10,000 Global Functions: Working with Legacy JavaScript
jQuery & 10,000 Global Functions: Working with Legacy JavaScriptGuy Royse
 

Similar a Styling components with JavaScript (20)

Practical HTML5: Using It Today
Practical HTML5: Using It TodayPractical HTML5: Using It Today
Practical HTML5: Using It Today
 
Javascript first-class citizenery
Javascript first-class citizeneryJavascript first-class citizenery
Javascript first-class citizenery
 
[Coscup 2012] JavascriptMVC
[Coscup 2012] JavascriptMVC[Coscup 2012] JavascriptMVC
[Coscup 2012] JavascriptMVC
 
Guia de Sobrevivência JS no mundo Open Source
Guia de Sobrevivência JS no mundo Open SourceGuia de Sobrevivência JS no mundo Open Source
Guia de Sobrevivência JS no mundo Open Source
 
Wt unit 2 ppts client sied technology
Wt unit 2 ppts client sied technologyWt unit 2 ppts client sied technology
Wt unit 2 ppts client sied technology
 
Wt unit 2 ppts client side technology
Wt unit 2 ppts client side technologyWt unit 2 ppts client side technology
Wt unit 2 ppts client side technology
 
Web performance essentials - Goodies
Web performance essentials - GoodiesWeb performance essentials - Goodies
Web performance essentials - Goodies
 
Having Fun with Play
Having Fun with PlayHaving Fun with Play
Having Fun with Play
 
Web Development for UX Designers
Web Development for UX DesignersWeb Development for UX Designers
Web Development for UX Designers
 
Webpack
Webpack Webpack
Webpack
 
RequireJS
RequireJSRequireJS
RequireJS
 
Jquery presentation
Jquery presentationJquery presentation
Jquery presentation
 
Jarv.us Showcase — SenchaCon 2011
Jarv.us Showcase — SenchaCon 2011Jarv.us Showcase — SenchaCon 2011
Jarv.us Showcase — SenchaCon 2011
 
J query b_dotnet_ug_meet_12_may_2012
J query b_dotnet_ug_meet_12_may_2012J query b_dotnet_ug_meet_12_may_2012
J query b_dotnet_ug_meet_12_may_2012
 
TDC2017 | Florianopolis - Trilha DevOps How we figured out we had a SRE team ...
TDC2017 | Florianopolis - Trilha DevOps How we figured out we had a SRE team ...TDC2017 | Florianopolis - Trilha DevOps How we figured out we had a SRE team ...
TDC2017 | Florianopolis - Trilha DevOps How we figured out we had a SRE team ...
 
Django Rest Framework and React and Redux, Oh My!
Django Rest Framework and React and Redux, Oh My!Django Rest Framework and React and Redux, Oh My!
Django Rest Framework and React and Redux, Oh My!
 
Integrating React.js Into a PHP Application
Integrating React.js Into a PHP ApplicationIntegrating React.js Into a PHP Application
Integrating React.js Into a PHP Application
 
Assetic (Zendcon)
Assetic (Zendcon)Assetic (Zendcon)
Assetic (Zendcon)
 
Javascript unit testing, yes we can e big
Javascript unit testing, yes we can   e bigJavascript unit testing, yes we can   e big
Javascript unit testing, yes we can e big
 
jQuery & 10,000 Global Functions: Working with Legacy JavaScript
jQuery & 10,000 Global Functions: Working with Legacy JavaScriptjQuery & 10,000 Global Functions: Working with Legacy JavaScript
jQuery & 10,000 Global Functions: Working with Legacy JavaScript
 

Último

Introduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxIntroduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxupamatechverse
 
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130Suhani Kapoor
 
UNIT-II FMM-Flow Through Circular Conduits
UNIT-II FMM-Flow Through Circular ConduitsUNIT-II FMM-Flow Through Circular Conduits
UNIT-II FMM-Flow Through Circular Conduitsrknatarajan
 
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCollege Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCall Girls in Nagpur High Profile
 
Porous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingPorous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingrakeshbaidya232001
 
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 
Extrusion Processes and Their Limitations
Extrusion Processes and Their LimitationsExtrusion Processes and Their Limitations
Extrusion Processes and Their Limitations120cr0395
 
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Dr.Costas Sachpazis
 
Software Development Life Cycle By Team Orange (Dept. of Pharmacy)
Software Development Life Cycle By  Team Orange (Dept. of Pharmacy)Software Development Life Cycle By  Team Orange (Dept. of Pharmacy)
Software Development Life Cycle By Team Orange (Dept. of Pharmacy)Suman Mia
 
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete RecordCCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete RecordAsst.prof M.Gokilavani
 
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).pptssuser5c9d4b1
 
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service NashikCall Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service NashikCall Girls in Nagpur High Profile
 
KubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghlyKubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghlysanyuktamishra911
 
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...ranjana rawat
 
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...Call Girls in Nagpur High Profile
 
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...Christo Ananth
 
Microscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxMicroscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxpurnimasatapathy1234
 

Último (20)

Introduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxIntroduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptx
 
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
 
UNIT-II FMM-Flow Through Circular Conduits
UNIT-II FMM-Flow Through Circular ConduitsUNIT-II FMM-Flow Through Circular Conduits
UNIT-II FMM-Flow Through Circular Conduits
 
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
 
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCollege Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
 
Porous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingPorous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writing
 
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINEDJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
 
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
Extrusion Processes and Their Limitations
Extrusion Processes and Their LimitationsExtrusion Processes and Their Limitations
Extrusion Processes and Their Limitations
 
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
 
Software Development Life Cycle By Team Orange (Dept. of Pharmacy)
Software Development Life Cycle By  Team Orange (Dept. of Pharmacy)Software Development Life Cycle By  Team Orange (Dept. of Pharmacy)
Software Development Life Cycle By Team Orange (Dept. of Pharmacy)
 
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete RecordCCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
 
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt
 
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
 
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service NashikCall Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
 
KubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghlyKubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghly
 
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
 
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
 
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
 
Microscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxMicroscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptx
 

Styling components with JavaScript

  • 1. Styling Components with JavaScript @bensmithett
  • 2. Warning » Not a tutorial for use in production! » I'm not even using any of this outside late night hacks But there are some interesting new ideas. Let's explore them & challenge CSS best practices!
  • 3. Components are awesome! Nobody builds pages any more. Here's an example Profile component: components/ Profile/ index.hbs index.css index.js
  • 4. HTML Template <div class="profile"> <img class="profile__avatar" src="{{avatarUrl}}.jpg" /> <strong>{{username}}</strong> </div> Style .profile { border: 1px solid #ddd; overflow: hidden; } .profile__avatar { float: left; margin-right: 10px; }
  • 5. Behaviour var Profile = function (el) { el.addEventListener("click", function () { console.log("hai!"); }); this.el = el; this.tmpl = Handlebars.compile(someTemplateString); }; Profile.prototype.render = function (state) { this.el.innerHTML = this.tmpl(state); };
  • 6.
  • 7. React combines HTML structure & behaviour var React = require("react"); var Profile = React.createClass({ handleClick: function () { console.log("hai"); }, render: function () { return ( <div class="profile"> <img class="profile__avatar" src="{this.props.avatarUrl}.jpg" onClick={this.handleClick} /> <strong>{this.props.username}</strong> </div> ); } }); module.exports = Profile;
  • 8.
  • 9. That's a big dirty lie The component's CSS is one of its concerns, but it's off in some random other file. components/ Profile/ index.css index.jsx
  • 10. The only connection: a class name // JS render: function () { return ( <div class="profile"> // ... </div> ) } /* CSS */ .profile border: 1px solid #ddd; overflow: hidden;
  • 11. Most things » JS Dependencies are explicitly required » HTML structure is right there in the file » JS behaviour is right there in the file CSS » In another file, the classes might have the same name ¯_()_/¯ It's a crappy, vague connection.
  • 12. CSS builds are a bit backwards //app.scss @import vendor/Normalize.css; @import base; @import components/Header; @import components/Profile; @import components/Footer; You need to know which bits of CSS your app requires. Lame.
  • 13. What if our JS build automatically created a stylesheet based only on the components we use? // app.js var Profile = require(./components/Profile); var Header = require(./components/Header); var Footer = require(./components/Footer); // app.css // Somehow... // components/Profile/index.css // components/Header/index.css // components/Footer/index.css // ... end up here?
  • 14.
  • 16. var React = require(react); require(./index.css); var Profile = React.createClass({ render: function () { return ( div class=profile / ); } }); module.exports = Profile;
  • 17. // app.js var Profile = require(./components/Profile); var Header = require(./components/Header); var Footer = require(./components/Footer); // app.css generated by webpack .profile { ... } .profile__avatar { ... } .header { ... } .footer { ... }
  • 18. Hooray! CSS is just another dependency we can require() in our component
  • 19. Hooray? components/ Profile/ index.css index.jsx » Still working across 2 files » Need to maintain class names across 2 files ... still a bit lame.
  • 20. React can do inline styles // Profile/index.js var Profile = React.createClass({ styles: { border: 1px solid #ddd, overflow: hidden }, render: function () { return( div style={this.styles} / ); } }); !-- DOM generated by React -- div style=border: 1px solid #ddd; overflow: hidden; /div
  • 21. Nobody likes inline styles though
  • 22. What we really want: » Declare styles in the component, like we do with inline styles » Build process that... » converts them to a CSS class » spits out a shiny, auto-generated app.css » component knows to use that class name
  • 23. React-style does that! » https://github.com/SanderSpies/react-style » http://andreypopp.com/posts/2014-08-06- react-style.html (with a little help from webpack)
  • 24. var React = require(react/addons); var ReactStyle = require(react-style); var Profile = React.createClass({ styles: ReactStyle(function () { return { backgroundColor: green, border: 1px solid #ddd }; }), render: function () { return( div styles={this.styles()} / ); } }); module.exports = Profile;
  • 25. !-- DOM generated by React -- div class=a ... /div // app.css generated by React-style Webpack .a { background-color: green; border: 1px solid #ddd; }
  • 26. Demo Compiling with default compressed class names
  • 28. Do you even need a CSS naming convention?
  • 29. not really... » Styles are tightly coupled part of the component, not a separate thing » CSS class naming conventions are a project setting, not an inherent property of the component » Dev: BEM class names for easy debugging » Prod: Tiny compressed class names
  • 30. I 3 Sass $red: #f00; $grid-columns: 12; $base-font-size: 16px; @function px-to-em($px, $base: $base-font-size) { @return ($px / $base) * 1em; } %placeholder { color: $red; } .thing { @extend %placeholder; padding: 10px; }
  • 31. What is a Preprocessor? A language that helps us generate blocks of key:value pairs. selector { property: value; other-property: other-value; }
  • 32. What is a Preprocessor? A language that helps us generate blocks of key:value pairs. selector = { property: value, other-property: other-value }; JavaScript can do that!
  • 33. JS already has lots of Real Programming Language Things TM » Variables » Functions » Arrays Objects » Loops » Maths » String manipulation » Dependency management
  • 34. Warning! Total pseudocode, nothing past this point actually works
  • 35. Example: color variables var colors = require(./color_palette); var Profile = React.createClass({ styles: ReactStyle(function () { return { color: colors[hotPink], }; }), render: function () { return( div styles={this.styles()} / ); } });
  • 36. Example: Generate a grid var gridColumns = 12; var styles = {}; for (var i = 1; i = gridColumns; i++) { var width = (i / gridColumns) * 100; styles[span- + i] = ReactStyle(function () { return { float: left, width: width + % } }); } var GridColumn = React.createClass({ styles: styles, render: function () { var columns = span- + this.props.columns; return( div styles={this.styles[columns]()} / ); } });
  • 38. The end :) @bensmithett https://github.com/bensmithett/react-style-example https://github.com/SanderSpies/react-style https://github.com/chenglou/rcss https://github.com/hedgerwang/react-styles https://github.com/elierotenberg/react-css