Comments (3)
First: a quick update from what I posted in polished. Last night I switched the implementation from canvas
to getComputedStyle
because the performance is much better.
Being unfamiliar with the intricacies of how canvas interacts with the DOM, any limitations with this approach when using SSR rendering?
I have a solve for this. If you look at the code, there is a hidden require
in there that should only be run in non-browser environments.
color2k/packages/parse-to-rgba/src/index.ts
Lines 27 to 36 in a48bf58
When that branch runs, it will parse the color using the optional node compatibility package @color2k/node
@color2k/compat
(which is a blatant copy-paste of polished 🙌) instead of going down the path of using the browser to parse colors.
In polished, folks tend to either use our babel-plugin or others that preval functions where possible to remove the runtime impact and reduce bundle size. Given the canvas approach, I imagine there would need to be a custom plugin to render the canvas and do the work to precompile?
Given this approach, I think this will work fine in all node environments? I only tried this lib out in Gatsby and it worked flawlessly there but I still think I have more work to do regarding testing this thing.
Now that you remind me though, I forgot to test out the new getComputedStyle
implementation in jsdom. I just added those tests and was greeted with this 😅:
In summary, I think the shim + hidden require works pretty well but there's a decent amount of effort in testing this in non-node environments but at least playwright is a big help.
from color2k.
…and JSDOM is now supported!
from color2k.
I know this was posted as a question but I'd like to turn this issue into an actionable todo list in order to ensure this lib works in all non-browser environments (probably just testing locally). I don't expect any issues but it would be good to know before stamping the 1.0 on it.
To summarize (and to be referenced later in the README), in the browser, color2k will use getComputedStyle
. In non-browser/SSR setups, an inline require
will pull the compat package from node_modules
. Because the require
is inline, this compat package will not be included in the browser's bundle.
Here's a list environments/setups I've tested and what is left to test:
- safari/webkit (via playwright) ✅
- chrome/edge (via playwright) ✅
- firefox (via playwright) ✅
- old edge
- IE 11
- codesandbox ✅
- create-react-app ✅
- gatsby ✅ — requires
@color2k/compat
for SSR - node ✅ — requires
@color2k/compat
- jest + jsdom ✅ — requires
@color2k/compat
- next.js — will probably require
@color2k/compat
for SSR - react-native — will probably require
@color2k/compat
- preval — will probably require
@color2k/compat
- vue/vue-cli
- angular/angular-cli
- svelte/svelte-cli
- deno
A final note on DX: The experience I'm going for is that color2k
"just works" after an install. If it doesn't, then just install @colork2/compat
. The intricacies of browser vs non-browser should be handled by the lib.
from color2k.
Related Issues (20)
- Try switching to the `exports` entry point instead of `modules` HOT 1
- Action Required: Fix Renovate Configuration
- Package maintenance: semantic-release and microbundle HOT 1
- The main field in the package.json of `[email protected]` points to non-existent TypeScript file HOT 4
- Doesn't work on node 6 HOT 9
- Node 6 Compatibility Suite / Migrate to `tape` HOT 1
- Bundled outputs HOT 1
- IsDark and isLight support? I can not find HOT 1
- Dependency Dashboard
- `mix` returns `NaN` values at extremes with fully transparent colors HOT 1
- node 4 compatibility HOT 1
- Color conversion problem HOT 1
- [Feature] Invert color HOT 1
- [Feature] add support for hex8 HOT 2
- Support space separated rgb values HOT 3
- Using `toHex` with `hsla` where `a = 0` leads to a result with alpha=1 HOT 3
- Does not work with next.js SSR HOT 1
- parseToRgba can't handle percentage alpha values
- Breaks in the latest webkit version HOT 3
- How to verify if a string is a valid color value?
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from color2k.