daeds-atomic
A small command line utility for using atomic css classes in your markup, heavily influenced from acss.info. It will create a css file based on the classes found in the folders it monitors.
Using webpack?
Then try daeds-atomic-webpack-plugin instead.
Installation
npm install -g daeds-atomic
Configuration
The first thing you need to do is to create a file named daeds-atomic.json in the root of your project. Below is an example file:
folders
Array where you specify what folders daeds-atomic should listen for file changes in. Mandatory field.
fileExtensions
Array specifying what file extensions to monitor. Mandatory field.
output
String containing the relative path (from your root folder) where to put the resulting css file. Mandatory field.
mobileFirst
Boolean value specifying if your responsive classes should be mobile first or not, if not explicitly set in class name. Defaults to false.
variables
Hash where you can specify variable names along with an arbritary value. The variable names can be used in class names with an @-prefix.
overrides
Hash where you can override any of the predefined class prefixes, or add new ones. Based on the example above, you could in your markup write a class named custom_20px which would result in .custom_20px { margin: 20px; }
verbose
Boolean value indicating verbose output from the utility.
alwaysInclude
Array of class names that will always be outputted in the resulting css file, no matter if the classes are found or not.
Usage
When you have created the config file, just run the following command in your project root and you are ready to go:
daeds-atomic --watch
Add the --watch (or just -w) argument to also monitor for file changes.
Basics
The principle is that you write your styling directly in your markup as css classes with the following format:
_[:][_at[Min|Max]][!]
These classes are picked up and a css file is created automatically, consisting of the classes with their respective css rule.
Simple example
Say that you want a 20px margin on some element in your markup. To achieve this, use the following class:
Hello world
This markup would create a css file with the following content:
Let's say you also want a 10px padding on the element + a red background-color when a user hovers over the element. To do this, add the following:
Hello world
This would update the css file to look like this:
Finally, you also want the element to have a 50% width when the browser window is less than 700px. Assuming the mobileFirst-property in the config is false, let's add a class for that:
Hello world
And this is the resulting css:
{}
Class prefixes
All atomic classes start with a prefix name in lower case followed by an underscore. The prefix dictates what css property name should be used. Below are the default class name prefixes. If you don't like them or if you miss a certain css property, you can easily override them or add new ones in the config.
b = border bb = border-bottom bbc = border-bottom-color bbs = border-bottom-style bbw = border-bottom-width bc = border-colorbg = background bgc = background-colorbl = border-left blc = border-left-colorbls = border-left-style blw = border-left-widthbottom = bottom boxsizing = box-sizingbr = border-right brc = border-right-colorbrs = border-right-style brw = border-right-widthbs = border-style bshadow = box-shadowbt = border-top btc = border-top-colorbts = border-top-style btw = border-top-widthbw = border-width clr = clearcolor = color cursor = cursord = display f = floatff = font-family fs = font-sizefstyle = font-style fw = font-weighth = height left = leftlh = line-height ls = list-stylem = margin maxh = max-heightmaxw = max-width mb = margin-bottomminh = min-height minw = min-widthml = margin-left mr = margin-rightmt = margin-top o = overflowop = opacity p = paddingpb = padding-bottom pl = padding-leftpo = position pr = padding-rightpt = padding-top radius = border-radiusright = right td = text-decorationtl = table-layout to = text-overflowtop = top trf = transformtrs = transition tshadow = text-shadowva = vertical-align w = widthws = white-space ww = word-wrapzi = z-index ta = text-alignol = outline ox = overflow-xoy = overflow-y
Css values in classes
After the class prefix, just enter any css value. Some key points:
- Use underscores for spaces, for example m_10px_20px will result in .m_10px_20px { margin: 10px 20px; }
- Keep in mind there is no validation what so ever. So if you enter a class called m_blah, that will result in .m_blah { margin: blah; } in the css output.
- You can use the following special characters in your class names (which will be escaped in the resulting css file): % . , ( ) @ # :
Pseudo classes
Pseudo classes are defined as a colon followed by the pseudo class name. For example b_none:last-child or bgc_red:hover.
Variables
You can define variables in the daeds-atomic.json config file (se example file above for syntax). To use a variable, just incude the variable name in a class name, with a @-prefix. So if you for example have a variable named standardPadding, you can use it as follows: p_@standardPadding. Keep in mind that variable names can only contain lowercase and uppercase letters and digits.
Responsive classes
Add classes defined within responsive breakpoints using the following suffix for the class name (where breakpoint is a variable or a value in px, em or rem):
_at[Min|Max]
Implicit mobile-first / desktop-first breakpoints
If you leave out the Min or Max in your class name, the mobileFirst-property in the daeds-atomic.json config file will decide if the class will be mobile-first or desktop-first. For example:
m_10px_at700px or p_10px_at30em.
Explicit mobile-first / desktop-first breakpoints
If you specify the Min or Max in your class name, it will control if the class will be mobile-first (Min) or desktop-first (Max). For example:
m_10px_atMin700px or p_10px_atMax30em.
Variable as breakpoint
You can use variables from the config file as breakpoint. For example if you have a variable named "smallScreen" you could use it as follows:
m_20px_at@smallScreen or m_20px_atMin@smallScreen m_20px_atMax@smallScreen
!important
Add a ! at the end of a class name to make the rule !important. For example:
m_20px!