question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Add an option to output atomic css

See original GitHub issue

Do you want to request a feature or report a bug? Feature

What is the current behavior?

If the current behavior is a bug, please provide the steps to reproduce and a minimal repository on GitHub that we can yarn install and yarn test.

What is the expected behavior? The current behavior is

// Input 

const header = css`
  text-transform: uppercase;
  font-family: ${fonts.heading};
  font-size: ${modularScale(2)};
`;

// Current Output
const header = 'header_t1ugh8t9';

// Expected Output
const header = 'tt_swer3 ff_Sers34 fs_5tsdf';
/*
tt_swer3  -> text-transform
ff_Sers34 -> font-family
fs_5tsdf -> font-size
*/

Please provide your exact Babel configuration and mention your Linaria, Node, Yarn/npm version and operating system.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:24
  • Comments:30 (15 by maintainers)

github_iconTop GitHub Comments

16reactions
jpnelsoncommented, Oct 4, 2021

This is a really interesting discussion – I’ve done a bit of a prototyping into what would be required for supporting this in Linaria – it’d be a fairly different implementation, but we could keep a similar API shape (not too dissimilar from css-zero ). At a high level, what I’d suggest would be something like this in React:

import { css, styled } from '@linaria/atomic';

const blue = css`
   color: blue;
`;

const Component = styled.div`
  color: blue;
  border: 1px solid black;
`;

export default function () {
  return <Component>
    <p className="blue">Hello</p>
  </Component>
}

which would produce:

<div class="atm_foo123">
  <p class="atm_foo123 atm_bar456">
    Hello
  </p>
</div>
.atm_foo123 {
  color: blue;
}
.atm_bar456 {
  border: 1px solid black;
}

To get there we’d need:

  • A @linaria/atomic package, that contains the new API (note: we’d probably also want new stylelint rules to enforce the fact that nesting / cascading isn’t allowed in atomic css)
  • Support in the babel plugin for the atomic logic in this new API
  • The collect utility from @linaria/server to support sorting / deduping these atomic rules (I think prefixing them with a consistent prefix, eg. atm_ could help here)

For @jayu 's point about deduping the produced rules, I think we dedupe rules at a file level, and rely on the bundler to dedupe those as well in produced css files (so, add webpack support, or similar).

For us, the big advantage of doing this is that the atomic rules don’t depend on declaration order – we can determine which atomic rules to apply at build time. Additionally, the total css size can be reduced.

It’d be a fairly big new addition to the library though – @jayu, if I created a proof of concept along the lines of the above, would you be open to discussing the potential inclusion? I think it’ll end up being a bunch of new work though so it’d be great to hear what your thoughts are for the direction on this going forward.

7reactions
LucaColonnellocommented, May 12, 2020

I would have loved to see this coming though! So sad to see it has been closed! This is the approach taken by the new Facebook style lib, which would be open source at some point. But I think, considering it seems that lib is quite opinionated in the way to write CSS (ReactNative style), it would be good to see this in Linaria as an opt-in feature.

If I have some time, does anyone mind me spiking it?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Let's Define Exactly What Atomic CSS is
This technique is a way to allow utility classes to override styles at different breakpoints to make implementation of responsive web design ...
Read more >
Atomic CSS - Compiled CSS in JS
Atomic CSS. Compiled outputs your styles as atomic CSS. This page goes over what it is and what behaviors are enabled by using...
Read more >
Frequently Asked Questions - Atomic CSS
Atomic CSS is a collection of single purpose styling units (single responsibility principle for maximum reuse) that fits well with components in templated ......
Read more >
How to write Atomic (design) CSS with Sass in Angular2
ts and then @import all your other scss files into this one. Make sure to remove the styles property from all of your...
Read more >
How to Dynamically Layout a Block using Atomic CSS Classes
Element uses Atomic CSS, a layout-focused CSS library that is designed for optimum performance on VOLT storefronts. When building a custom Block with...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found