TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Show HN: Styled-components – Use the best of ES6 to style React apps

172 pointsby mxstbrover 8 years ago

17 comments

cel1neover 8 years ago
I want to mention my styling library here: <a href="https:&#x2F;&#x2F;www.npmjs.com&#x2F;package&#x2F;react-native-style-tachyons" rel="nofollow">https:&#x2F;&#x2F;www.npmjs.com&#x2F;package&#x2F;react-native-style-tachyons</a><p>The best part: it gives you a consistent spacing [0] and a consistent font scale [1] which is relative to rem. No more magic values, just use classes:<p><pre><code> ma0 ... ma7 margin: 0|0.25|0.5|1|2|4|8|16 rem ml|mr|mb|mt [0-7] marginLeft, marginRight, marginBottom, marginTop mh [0-7] marginHorizontal mv [0-7] marginVertical &#x2F;* Same with p for padding *&#x2F; </code></pre> No more stylesheets, everything is there at a glance and easily fixable:<p><pre><code> &lt;View cls=&quot;bt bb jcfs pa2&quot;&gt; &#x2F;* border at top and bottom, justifyContent: stretch, padding: 0.5rem *&#x2F; &lt;Text cls=&quot;white tc&quot;&gt; &#x2F;* white color, text-align: center *&#x2F; Something &lt;&#x2F;Text&gt; &lt;&#x2F;View&gt; </code></pre> [0] <a href="http:&#x2F;&#x2F;tachyons.io&#x2F;docs&#x2F;layout&#x2F;spacing&#x2F;" rel="nofollow">http:&#x2F;&#x2F;tachyons.io&#x2F;docs&#x2F;layout&#x2F;spacing&#x2F;</a><p>[1] <a href="http:&#x2F;&#x2F;tachyons.io&#x2F;docs&#x2F;typography&#x2F;scale&#x2F;" rel="nofollow">http:&#x2F;&#x2F;tachyons.io&#x2F;docs&#x2F;typography&#x2F;scale&#x2F;</a>
评论 #12699828 未加载
评论 #12702302 未加载
评论 #12699875 未加载
评论 #12700191 未加载
评论 #12700621 未加载
评论 #12700195 未加载
nmalagutiover 8 years ago
For anyone unfamiliar with the syntax used to achieve this, it&#x27;s called Tagged Template Literals [0] and it&#x27;s part of ES2015. You have a function that receives the static strings and interpolations and can return anything you want.<p>[0] <a href="https:&#x2F;&#x2F;developer.mozilla.org&#x2F;en-US&#x2F;docs&#x2F;Web&#x2F;JavaScript&#x2F;Reference&#x2F;Template_literals" rel="nofollow">https:&#x2F;&#x2F;developer.mozilla.org&#x2F;en-US&#x2F;docs&#x2F;Web&#x2F;JavaScript&#x2F;Refe...</a>
评论 #12702542 未加载
andreypoppover 8 years ago
I think using strings as an API is a step in the wrong direction. I like that it produces a React component directly though.<p>We experimented with styling approaches for React a while ago and produced our solution — React Stylesheet[1], which we used for over an year in different React applications.<p>It also produces components out of style definitions directly:<p><pre><code> let GreenBox = style(&#x27;div&#x27;, { base: { background: &#x27;green&#x27; } }) </code></pre> Which you can use like any other React component later:<p><pre><code> &lt;GreenBox &#x2F;&gt; </code></pre> As you can see the API is kinda similar but uses JS instead of CSS string. What are advantages? The following:<p>* Consistent syntax: you can use functions, variabls, mixins, w&#x2F;o the need for interpolation `${...}`.<p>* Tooling. This is big one. You can make your stylesheets linted with ESLint and typesafe (!!!) with FlowType or TypeScript.<p>FlowType is supported out of the box, see [2]. Typesafe stylesheets is a huge win for productivity, I constantly catch typos and invalid values via Flow.<p>[1]: <a href="https:&#x2F;&#x2F;github.com&#x2F;prometheusresearch&#x2F;react-stylesheet&#x2F;blob&#x2F;next&#x2F;README.md" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;prometheusresearch&#x2F;react-stylesheet&#x2F;blob&#x2F;...</a> [2]: <a href="https:&#x2F;&#x2F;github.com&#x2F;prometheusresearch&#x2F;react-stylesheet&#x2F;blob&#x2F;next&#x2F;README.md#type-safety" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;prometheusresearch&#x2F;react-stylesheet&#x2F;blob&#x2F;...</a><p>EDIT: additions<p>Styles are compiled into CSS so :hover and friends work here.
评论 #12700779 未加载
drinchevover 8 years ago
What is the performance penalty of using generated styles on every render?<p>As far as I can see from the source code, React will have to modify `&lt;style&gt;` tag every time a component is being rendered.<p>I still think the best way to handle stylesheets is with external css file ( doesn&#x27;t matter how it&#x27;s generated. I use CSSModules ) and then add static class names for those components.
评论 #12700280 未加载
评论 #12705650 未加载
pgtover 8 years ago
After Reagent + Hiccup, I&#x27;m ruined. Nothing feels as natural as a full, general-purpose language (Clojure) combined with a natural keyword-based DSL for styling:<p><pre><code> (defn right-cell [&amp; body] [:td.right.aligned body]) ;; renders &lt;td class=&quot;right aligned&gt;{...body}&lt;&#x2F;td&gt; (defn person-row [{:keys [name age] :as p}] [:tr [:td name] [right-cell age]]) (defn person-table [font-size people] (let [heading-size (* font-size 1.6)] [:div [:h1 {:style {:font-size (str heading-size &quot;em&quot;)} &quot;People List&quot;] [:table.ui.basic.table [:tbody (map person-row people)]]])) (defn main [] [person-table 2 [{:name &quot;John&quot; :age 21} {:name &quot;Sally&quot; :age 22}])</code></pre>
z3t4over 8 years ago
With components I think we are taking a step back from semantic HTML elements, where style is applied by a separate template. And business logic is also separated from the layout elements.
评论 #12702152 未加载
评论 #12702440 未加载
iosephover 8 years ago
I&#x27;m probably missing something but what&#x27;s the advantage of this over usual way of defining styles in React?
评论 #12699814 未加载
elecenginover 8 years ago
How do you do pseudo classes like :focus?
评论 #12699991 未加载
jeswinover 8 years ago
glamor is another option for React apps, if inline styling is your thing. <a href="https:&#x2F;&#x2F;github.com&#x2F;threepointone&#x2F;glamor" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;threepointone&#x2F;glamor</a>
评论 #12700179 未加载
cfvover 8 years ago
Neat. Now, why not actual CSS?
评论 #12700301 未加载
评论 #12701950 未加载
anilgulechaover 8 years ago
What&#x27;s the font in the code-editor screenshot? The italics look good!
评论 #12699925 未加载
评论 #12699926 未加载
andrewvijayover 8 years ago
Great one! Would love to do this in my side projects.
评论 #12700334 未加载
himlionover 8 years ago
Saw your talk this morning at reactnl, cool stuff!
评论 #12700447 未加载
Kiroover 8 years ago
What about media queries?
评论 #12700033 未加载
Raphmediaover 8 years ago
Emoji as CSS classes. Never thought of that one...
gjolundover 8 years ago
Very cool, I have been playing around with this idea for a little bit now and your implementation looks solid.<p>Looking forward to giving this a shot on my next side project.
retoxover 8 years ago
Site is a blank page for me.
评论 #12700216 未加载