Turn a JavaScript object with CSS styles into a class in a stylesheet.
var buttonClass = mx.style({
color: "yellow",
padding: 10,
":hover" { color: "black" }
});
myButton.className = buttonClass;
// .mx__0 { color: yellow; padding: 10px }
// .mx__0:hover { color: black }
When writing an application where the DOM is generated by JavaScript, defining styles where you define your views are often convenient. Rather than applying de-facto inline styles with classes such as text-lowercase
and pull-left
, you might just as well embrace inline styles.
Using JavaScript to define your styles also gives you the full power of a familiar programming language to inherit, extend and manipulate your styles.
The style
attribute has some limitations though, like the inability to add pseudo classes. Manipulating many individual style properties can also have performance implications. marcssist lets you write class-less styles, just as Marx intended, without giving away the power of CSS.
While created with React in mind, any JavaScript code/framework/library that accepts a class name when creating an element should work perfectly fine with marcssist.
marcssist exposes itself as a CommonJS module if in such an environment, and otherwise on window.marcssist
. I always alias that to mx
for convenience.
Add a style object, returning a class name used to apply the styles. If style
is an array, the properties are added left-to-right, with the properties on the last object overriding previous ones.
mx.style([{ height: 1, width: 1 }, { width: 2 }]);
// .mx__1 { height: 1px; width: 2px }
If an object is passed as a value to a key, the key is considered a selector instead of a CSS property. It is simply treated as a new style block, but with the key appended to the returned class name. All selectors not starting with :
or [
will automatically be separated by a space. You can nest styles as deep as you like.
mx.style({ background: "red", "a:hover": { "> span b": { color: "blue" }}});
// .mx__2 { background: red }
// .mx__2 a:hover > span b { color: blue }
Similar to the style
method, values can also be an array of objects.
var buttonStyle = { textDecoration: "none", border: "1px solid grey" }
mx.style({ a: [ buttonStyle, { color: "blue" }]});
// .mx__3 a { text-decoration: none; border: 1px solid grey; color: blue }
Create a new instance of marcssist with the supplied options. Valid options and their defaults are:
- Vendor prefixing:
prefix = true
- Auto units:
unit = "px"
The current vendor prefix is automatically detected, and then added to a number of pre-defined properties and property/value combinations. If you're missing a property, you can add it to the marcssist._prefixed(Props|Values)
objects. See source code for format.
mx.style({ columnCount: 2 }});
// .mx__3 { -webkit-column-count: 2 }
Support for prefixed values is currently limited.
If a number is specified, it will be converted to a string with an appended unit. This is convenient for things like { padding: PADDING + 5 }
.
What's remaining feature-wise is @at-rules, improved browser compatibility and performance. If you want other features, you should probably use a separate library.
Send a pull request, with usage examples, to add more libraries suitable for marcssist users.
mx.style({ background: tinycolor("hotpink").lighten() });
Copyright (c) 2014 Jacob Rask, <jacob@jacobrask.net>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.