

GitHub - cristianbote/goober: ? goober, a less than 1KB ?css-in-js alternative w...
source link: https://github.com/cristianbote/goober
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

README.md
? goober, a less than 1KB css-in-js solution.
Motivation
I always wondered, if you can get a working solution for css-in-js with a smaller footprint. I started a project and wanted a to use styled-components. Looking at their sizes, it seems that I would rather not include ~16kB(styled-components) or ~11kB(emotion) just so I can use the styled
paradigm. So, I embarked in a mission to create a smaller alternative for these well established apis.
Usage
The API is inspired by emotion, styled
function. Meaning, you call it with your tagName
and returns a vDOM component for that tag. Note, setPragma
is needed to be run before the styled
function is used.
import { h } from "preact"; import { styled, setPragma } from "goober"; // Should be called here, and just once setPragma(h); const Icon = styled("i")` display: flex; flex: 1; color: red; `; const Button = styled("button")` background: dodgerblue; color: white; border: 0; &:focus, &:hover { padding: 1em; } .otherClass { margin: 0; } ${Icon} { color: black; } `;
Examples
SSR
You can get the critical CSS for SSR, via extractCss
. Take a look at this example: CodeSandbox: SSR with Preact and goober
API
As you can see it supports most of the syntaxes of CSS. If you find any issues, please submit a ticket or even a PR with a fix.
styled(tagName)
@param {String} tagName
The name of the dom element you'd like the styled to be applied to@returns {Function}
Returns the tag template function.
import { styled } from "goober"; const Btn = styled("button")` border-radius: 4px; `;
setPragma(pragma: Function)
Given the fact that react
uses createElement
for the transformed elements and preact
uses h
, setPragma
should be called with the proper pragma function. This was added to reduce the bundled size and being able to bundle esmodule version. At the moment I think it's the best tradeoff we can have.
import React from "react"; import { setPragma } from "goober"; setPragma(React.createElement);
css(taggedTemplate)
@returns {Function}
Returns the tag template function.
Same as styled
but without the tagName and vNode generation. In the end the output will be a className.
import { css } from "goober"; const BtnClassName = css` border-radius: 4px; `(); // (!) Note the empty param at the end. If you wanna use `props` throughout the syntax this is the place to put them const btn = document.querySelector("#btn"); btn.classList.add(BtnClassName);
extractCss()
@returns {String}
Returns the <style>
tag should be rendered in your document <head>
and clears the cache.
const { extractCss } = require("goober"); // After your app has rendered, just call it: const styleTag = extractCss();
Features
- Basic CSS parsing
- Nested rules with pseudo selectors
- Nested styled components
- Media queries (@media)
- Keyframes (@keyframes)
- Smart(lazy) client-side hydration
- Vanilla(via
css
function) globalStyle
so one would be able to create global styles- Vendor prefixing
Contributing
Feel free to try it out and checkout the examples. If you wanna fix something feel free to open a issue or a PR.
Recommend
-
167
tipsy/bubbly-bg: Beautiful bubbly backgrounds in less than 1kB (750 bytes gzipped)...
-
75
Plain Javascript(no framework) Hacker News Example See this application live at: https://hnpwa-vanilla.firebaseapp.com This is an example of a PWA built using no...
-
9
<1kb virtual DOM - it's fast! Current Virtual DOM implementations are too complicated—Whether it be a fully featured yet bloated Virtual DOM in React, or the deprecated virtual-dom package on NPM, they are basically u...
-
8
-
7
-
12
An Ode to jQuery (and a 1kb alternative!)When jQuery came out it really was spectacular, it gave developers a common Vernacular, But times have changed and the year is 2021, and the time for jQuery has passed and gone....
-
5
Local Environment This is a cross-platform tool to spinup services for development from a unified configuration file similar to Docker Compose but the services run directly on the host instead of container. Thus no virtual machine is requ...
-
10
Making a Website Under 1kBAugust 02, 2022I recently launched (another) website club called the 1kB Club. Unlike the
-
2
How to Get 16k GitHub Stars in Less Than 2 Years 🚀 10 lessons on how we got to +16,000 GitHub stars in 16 months with Medusa. It ha...
-
3
This Apple Watch alternative costs less than $35 for a short time
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK