<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Dan Bahrami on Medium]]></title>
        <description><![CDATA[Stories by Dan Bahrami on Medium]]></description>
        <link>https://medium.com/@danbahrami?source=rss-48bbfc921420------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*42YGEMt3hjitFKvGfBoM0g.jpeg</url>
            <title>Stories by Dan Bahrami on Medium</title>
            <link>https://medium.com/@danbahrami?source=rss-48bbfc921420------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 21 Nov 2026 04:03:36 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@danbahrami/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[How we use React "Render props" at Geckoboard]]></title>
            <link>https://medium.com/geckoboard-under-the-hood/how-we-use-react-render-props-at-geckoboard-713bbfba4832?source=rss-48bbfc921420------2</link>
            <guid isPermaLink="false">https://medium.com/p/713bbfba4832</guid>
            <category><![CDATA[render-props]]></category>
            <category><![CDATA[reactjs]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[projects]]></category>
            <category><![CDATA[react]]></category>
            <dc:creator><![CDATA[Dan Bahrami]]></dc:creator>
            <pubDate>Mon, 11 Jun 2018 08:21:00 GMT</pubDate>
            <atom:updated>2018-06-11T08:21:00.844Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*qC39jwPw5NEE8sJ2__o_4Q.png" /></figure><h3>How we use React “Render props” at Geckoboard</h3><p>I first heard the term “Render props” in 2016 when I attended a React Training event here in London, run by <a href="https://twitter.com/mjackson">Michael Jackson</a>. At first I didn’t understand why it was helpful but, in the years since then, I have found the pattern useful in so many cases. Everyone seems to be talking about Render props at the moment so I thought I’d share some real components from our codebase at Geckoboard to illustrate how we use it.</p><p>I’m not going to cover what the Render props pattern is in this article because there are tons of great articles that explain that already. If you would like to learn more, I recommend watching <a href="https://www.youtube.com/watch?v=BcVAq3YFiuc">Michael’s excellent conference talk</a> about HOCs and Render props.</p><h4>1. Feature Flagging</h4><p>We often use feature flags to test new, experimental features and to ship those features to a limited set of customers. Our &lt;FeatureFlag /&gt; component allows us to conditionally render UI based on the customer’s feature flags.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e7a27dd87e1c01dd9a0263a6eb46d542/href">https://medium.com/media/e7a27dd87e1c01dd9a0263a6eb46d542/href</a></iframe><p>We store the feature flags in a <a href="http://npmjs.com/package/redux">Redux</a> store, internally this component acts like a Redux connector that passes the flags to its children but, by taking a function as a child, it makes our JSX nice and readable.</p><h4>2. Drag and Drop</h4><p>The Geckoboard UI allows you to drag and drop “widgets” around a “dashboard”. Our &lt;DraggableWidget /&gt; component wraps up all the logic for sensing drag events and updating the widget’s position relative to the dashboard. This way our widget component can focus purely on rendering its content.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e1ece016faa024b4b88414a958df18a5/href">https://medium.com/media/e1ece016faa024b4b88414a958df18a5/href</a></iframe><h4>3. Full-screen events</h4><p>People want to display their Geckoboards on a TV Screen without having the browser toolbar visible. Luckily, all good browsers offer some full-screen functionality to make that possible. Our &lt;Fullscreen /&gt; component lets its child know if the browser is currently in full-screen mode and offers two functions it can call to enter and exit full-screen.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/7fb8c29bf92e214a8be0ea46a4722f92/href">https://medium.com/media/7fb8c29bf92e214a8be0ea46a4722f92/href</a></iframe><p>We have one of these wrapping our dashboard container component so that it can display itself differently in full-screen mode <em>(In full-screen we scale the dashboard to fill the screen and show a footer containing the dashboard title, a company logo and a clock)</em>. We have another wrapping the full-screen button, as shown in the example above, so the customer can toggle full-screen mode on and off.</p><p>Under the hood we use the <a href="https://www.npmjs.com/package/fscreen">fscreen</a> library to make our full-screen functionality behave cross-browser.</p><p>These are three examples in a growing list of Render prop components we use in production at Geckoboard. Render props are a great way of extracting complexity into self-contained components, that you can use throughout your React codebase, while maintaining clean, readable JSX.</p><p>I hope this post gives you some inspiration for how to use the Render props pattern. Feel free to post your components in the comments, we’d be interested in seeing them!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=713bbfba4832" width="1" height="1" alt=""><hr><p><a href="https://medium.com/geckoboard-under-the-hood/how-we-use-react-render-props-at-geckoboard-713bbfba4832">How we use React &quot;Render props&quot; at Geckoboard</a> was originally published in <a href="https://medium.com/geckoboard-under-the-hood">Geckoboard: Under The Hood</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How we made our product more personalized with CSS Variables and React]]></title>
            <link>https://medium.com/geckoboard-under-the-hood/how-we-made-our-product-more-personalized-with-css-variables-and-react-b29298fde608?source=rss-48bbfc921420------2</link>
            <guid isPermaLink="false">https://medium.com/p/b29298fde608</guid>
            <category><![CDATA[projects]]></category>
            <category><![CDATA[development]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[react]]></category>
            <category><![CDATA[startup]]></category>
            <dc:creator><![CDATA[Dan Bahrami]]></dc:creator>
            <pubDate>Mon, 10 Apr 2017 16:12:52 GMT</pubDate>
            <atom:updated>2018-04-05T15:35:24.853Z</atom:updated>
            <content:encoded><![CDATA[<p>I have seen some buzz around <a href="https://developer.mozilla.org/en-US/docs/Web/CSS/--*">CSS Variables</a> lately and recently had the opportunity to use them as part of the front-end team at <a href="https://www.geckoboard.com/">Geckoboard</a>. They solved a very specific problem for us while being easy to use, fast, and maintainable. In this post, I’ll describe how and why we used CSS Variables in combination with React to re-style our web app on the fly!</p><h4>The problem</h4><p>At <a href="https://medium.com/u/bfb03fc87209">Geckoboard</a>, we build live dashboards to display key metrics on TV screens. Being able to add your own personality to a dashboard is important to a lot of our customers, so a few months ago our design team previewed a feature they were calling “Customization”.</p><p>The idea was simple: a form where you tweak your dashboard with new background colors, widget colors, font-sizes etc. and we would display a live preview of what your dashboard will look like.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/640/1*dTladcIJvtmb7apgLJ4mWQ.gif" /><figcaption>The design prototype for Customization with live preview</figcaption></figure><p>As soon as the engineering team saw the “live preview” aspect, we knew we had a hurdle to jump. Our site is a <a href="https://facebook.github.io/react/">React</a> application and handling UI updates is easy, however all our styles are written in SASS, so changing the dashboard theme in such a reactive way would be a problem.</p><p>At this point we supported two themes: dark and light. Since the default theme was dark, we would override our main CSS by loading a light.css stylesheet whenever a user chose the light theme for their dashboard.</p><p>But now our design team wanted to support millions of possible themes and they needed to be applied without a page reload, in a performant way… we had to rethink what a “theme” was.</p><h4>These were our assumptions</h4><ol><li>Our web-app would contain a single set of styles that could support any possible theme.</li><li>A theme has a standard format and can be entirely defined in JSON. (this is how it will be stored in our database)</li><li>The applied theme can be changed client-side (easily), and when it is, our view layer re-renders automatically to fully represent the change.</li><li>The applied theme should be a top-down global concern, and not require opt-in from individual view components.</li><li>There will always be a default theme which the applied theme may partially or fully override.</li></ol><p>Here is an example of what a theme might look like in the new world…</p><pre>{<br>  &#39;theme-primary-color&#39;: &#39;#8DC63F&#39;,<br>  &#39;widget-color&#39;: &#39;#333333&#39;,<br>  &#39;dashboard-footer-text-color&#39;: &#39;#FFFFFF&#39;,<br>  // etc.<br>}</pre><h4>Sounds like a job for “CSS in JS”!</h4><p>Absolutely! This was a tempting option, combining inline styles with React would tick most of the boxes. Our styles would be flexible enough to support any theme, a JSON theme would be easy to parse and automatic re-renders would be handled by React.</p><p>On a fresh codebase, we definitely could have taken this route. However, at the time of writing, we have 29,951 lines of SASS in 566 files. Moving to inline styles would be one heck of change to make!</p><p>A few of us in the front-end team had done some reading around a new spec for native CSS Variables. It seemed interesting so we decided to build a proof of concept.</p><p><em>Note: There are some great frameworks around that handle styles in React. If this is an option for you then check out </em><a href="https://github.com/FormidableLabs/radium"><em>Radium</em></a><em>, </em><a href="https://styled-components.com/"><em>Styled Components</em></a><em> ,and </em><a href="https://www.npmjs.com/package/styled-jsx"><em>Styled JSX</em></a><em>.</em></p><h4>What are CSS Variables?</h4><p>CSS Variables (Or <a href="https://developer.mozilla.org/en-US/docs/Web/CSS/--*">CSS Custom Properties</a> as they are officially known) are a new feature of CSS that allow you to define placeholder values for CSS properties. The syntax of a variable name is a string starting with two dashes (e.g.--theme-primary-color), variables are then referenced using a new CSS function var(). Here’s what using a CSS Variable looks like…</p><pre>/* Define a variable at the top level of the cascade */<br>:root {<br>  --theme-primary-color: blue;<br>}</pre><pre>/* You can use this variable throughout your styles */<br>.header {<br>  background: var(--theme-primary-color);<br>}</pre><p>Now the background colour of my .header component will be whatever value I assign to the variable --theme-primary-color, in this case, ‘blue’. If at any time the variable updates, the browser repaints automatically and the updated style is applied.</p><p>In the above example the variable is assigned on the document’s <a href="https://www.w3schools.com/cssref/sel_root.asp">root selector</a>. Because this is the top level element, the value ‘blue’ will essentially be globally assigned to --theme-primary-color but variables can be redefined and also follow the cascade.</p><pre>:root {<br>  --theme-primary-color: blue;<br>}</pre><pre>.header {<br>  background: var(--theme-primary-color);<br>}</pre><pre>.sidebar {<br>  --theme-primary-color: red;<br>}</pre><pre>.sidebar .widget {<br>  background: var(--theme-primary-color);<br>}</pre><p>In this example the --theme-primary-color variable is reassigned inside .sidebar so that when it gets used by .widget inside .sidebar it has the value ‘red’. The effect of this is that our header will have a blue background and our sidebar widgets will have a red background.</p><p>In addition, if a variable is referenced but is never defined, or if the browser you’re using doesn’t support CSS Variables, then it gets ignored completely. So we can add a fallback for these cases quite easily…</p><pre>.header {<br>  background: blue; /* Fallback */<br>  background: var(--theme-primary-color);<br>}</pre><h4>Adding CSS Variables to our SASS</h4><p>This was a relatively easy move. In total there were 63 properties in our SASS that needed to change depending on the theme. To avoid having to manually add fallbacks in all these cases, we created a SASS mixin.</p><pre>@mixin variable($property, $variable, $fallback) {<br>  #{$property}: $fallback;<br>  #{$property}: var($variable);<br>}</pre><p>Then in our SASS we added the variables...</p><pre>/* what was... */<br>.dashboard {<br>  background: blue;<br>}</pre><pre>/* becomes... */<br>.dashboard {<br>  @include variable(background, --theme-primary-color, blue);<br>}</pre><pre>/* Which compiles to... */<br>.dashboard {<br>  background: blue;<br>  background: var(--theme-primary-color);<br>}</pre><p>At this point we had a product that looked exactly the same, because our CSS was always falling back to the default values we set, but we were ready to start applying CSS Variables!</p><h4>Using CSS Variables with React</h4><p>As I’ve mentioned, our UI is built in React, so we wanted a way to apply the theme with React. Considering our themes will be available as JavaScript objects, it is a perfect candidate to handle our 3rd assumption:</p><ul><li>The applied theme can be changed client-side (easily), and when it is, our view layer re-renders automatically to fully represent the change.</li></ul><p>If we had a component that took the theme as a prop and applied it as CSS Variables then React could take care of re-renders for us when the theme changed.</p><h4>How to apply CSS Variables in JavaScript</h4><p>So far I’ve talked about defining variables in CSS, but variables can also be defined with inline styles, either directly in HTML…</p><pre>&lt;div style=&quot;--theme-primary-color: blue;&quot;&gt;<br>  /* <br>   * For elements in here var(--theme-primary-color)<br>   * resolves to &#39;blue&#39;.<br>   */<br>&lt;/div&gt;</pre><p>or by using the <a href="https://developer.mozilla.org/en-US/docs/Web/API/CSSStyleDeclaration/setProperty">JavaScript setProperty api</a>…</p><pre>element.style.setProperty(&#39;--theme-primary-color&#39;, &#39;blue&#39;);</pre><p>We built a React component around the latter. It takes an object that maps variable names to values (that just so happened to be what a “theme” looks like) and applies them as style properties on the document’s root element. Here is a simplified version of that component…</p><pre>class CSSVariableApplicator extends Component {<br>  componentDidMount() {  <br>    this.updateCSSVariables(this.props.variables);<br>  }<br><br>  componentDidUpdate(prevProps) {<br>    if (this.props.variables !== prevProps.variables) {     <br>      this.updateCSSVariables(this.props.variables);<br>    }<br>  }</pre><pre>  updateCSSVariables(variables) {   <br>    forEach(variables, (value, prop) =&gt; {<br>      document.documentElement.style.setProperty(prop, value));<br>    });<br>  }</pre><pre>  render() {<br>    return &lt;div&gt;{this.props.children}&lt;/div&gt;;<br>  }<br>}</pre><p>As an implementation detail, we store our theme and handle updates with Redux, but here’s an example use without Redux…</p><pre>class App extends Component {<br>  constructor(props) {<br>    super(props);</pre><pre>    const theme = {<br>      &#39;--theme-primary-color&#39;: &#39;#8DC63F&#39;,<br>      &#39;--widget-color&#39;: &#39;#333333&#39;,<br>      &#39;--dashboard-footer-text-color&#39;: &#39;#FFFFFF&#39;,<br>    };</pre><pre>    this.state = { theme: theme };<br>  }</pre><pre>  render() {<br>    return (<br>      &lt;div&gt;<br>        &lt;CSSVariableApplicator variables={this.state.theme} /&gt;<br>        {* the rest of the app *}<br>      &lt;/div&gt;<br>    );<br>  }<br>}</pre><p>Guess what - it worked like a charm! I was shocked how easy it was to apply themes like this. When the theme is modified in state our component immediately applies the CSS Variables globally and the page is re-rendered with the new theme. That was a good feeling.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/640/1*e-3LOeDUbwR9rr4h7wyLrw.gif" /><figcaption>The working live-preview feature!</figcaption></figure><p>In this GIF there is a single, static CSS file, no styles are set inline and none of our React components (except the theme applicator) need to worry about the theme.</p><p>Even though our styles are still in CSS, we now have the full power of JavaScript to calculate theme values. In the screenshot above, you may notice that the widget background color is automatically selected based on the background color; this is not using transparency, we are calculating an appropriate color choice based on the background color.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/760/1*qwBxCQh8X52tgzc6QRlbUw@2x.png" /></figure><h4>The cons</h4><p>Of course, we can’t talk about new features of the CSS spec without browser support rearing its ugly head. Unfortunately, at the time of writing, there is no IE support at all for CSS Custom Properties.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*RSxwhWR_vHOXdqH81VnzyQ.png" /><figcaption>Browser support for CSS Custom Properties</figcaption></figure><p>Another downside is that our JavaScript and CSS are tied slightly more together now because they both need to be aware of what CSS variables we use. This is certainly a trade-off but it’s one we were willing to make.</p><h4>You should publish that React component to NPM!</h4><p>I already have!It’s called <a href="https://www.npmjs.com/package/react-custom-properties">react-custom-properties</a> and it is slightly more powerful than the example above. It can apply CSS Variables globally or to a specific part of your UI, it can also be nested so you can go crazy with CSS Variables!</p><h4><strong><em>Here’s </em></strong><a href="https://www.geckoboard.com/blog/new-personalize-your-dashboard-with-our-easy-customization-features/"><strong><em>more info</em></strong></a><strong><em> on our customization feature and how you can apply it to your Geckoboard dashboards! Also, </em></strong><a href="https://www.geckoboard.com/careers/"><strong><em>we’re hiring</em></strong></a><strong><em>!</em></strong></h4><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=b29298fde608" width="1" height="1" alt=""><hr><p><a href="https://medium.com/geckoboard-under-the-hood/how-we-made-our-product-more-personalized-with-css-variables-and-react-b29298fde608">How we made our product more personalized with CSS Variables and React</a> was originally published in <a href="https://medium.com/geckoboard-under-the-hood">Geckoboard: Under The Hood</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>