Consuming stitches at the "libary" level #534
-
Before any of the following, I want to say that stitches feels like a fantastic library and I've incredibly enjoyed utilizing it and seeing it grow over the past several months. ContextLike many others, we're working to build a design system with reusable artifacts for quickly building quality views. Most of the artifacts we have today are implemented as react components, with We've been looking to switch over to stitches for both the DX improvements and the ability for the component API to better mirror what say a Figma (or eventually, maybe Modulz 😄) option pane defines styles, as well as the performance improvements offered by avoiding prop interpolation at run-time. To this point, we've implemented a few packages with a common package consuming ProblemAs stitches isn't incredibly widespread, yet, this may not be something that the "app-level" consumer has run across yet, but I have no doubt they will as stitches continues to grow in popularity. From what I can tell, even with the addition of
Example of Problem ActualizedNote: the example below is outlined with an architecture similar to the use-case I describe above in
In the above dependency graph, the unpinned If at any point, >1 instance of Given stitches' api, I don't believe the thought of creating a reusable single-source-of-truth stitches config is far-fetched. I actually believe it to be one of the greatest strengths that stitches has to empower consumers, particularly in a way that hasn't been achievable with other CSS-in-JS solutions in the past without a great deal of ceremony or performance impact. Possible SolutionsAs it isn't possible or feasible to require the consumer to utilize the strict methods mentioned above for CSS encapsulation, I believe providing a mechanism, even an opt-in one, to facilitate library and application developers to utilize multiple instances of stitches within a single consuming entity would be highly beneficial to the stitches community 😄 Theoretically, I believe this limitation could be resolved in a relatively passive way to consumers through good, old-fashion hashing. Solution Litmus TestCan multiple instances of stitches and its utilities coexist within a single consuming entity without forcing consumers to implement strict style encapsulation via shadow DOM or other similar method, as doing so isn't always possible (borrowed from #472 in both of the following, but I'd actually be in favor of closing that issue in favor of a resolution to this discussion as I think resolving the issue described here could cover the cases described there as well) "Simple" hashing support
const { styled } = createCss({
hash: <user-defined-hash>,
theme: {
colors: {
red1: '#f00',
}
}
});
const Box = styled('div', {
color: 'blue',
}); with the resulting output being <style id="stitches-<hash>">
:root {
--colors-red1-<hash>: #f00;
}
/* unsure of where this class typically goes, but the class name itself is the only important part for this example */
.(stitches-normal-class-name-resolution)-<hash> { color: blue; }
</style> More Consumer Control Providing an optional
could allow for consumers and library developers greater flexibility in resolving the limitations noted above const stitchesConfig = createCss({
format: (name: string ) => `my-thing-${name}`,
});
const Box = styled('div', {
color: 'blue',
}); with the resulting output being <style id="my-thing-stitches">
:root {
--my-thing-colors-red1: #f00;
}
/* unsure of where this class typically goes, but the class name itself is the only important part for this example */
.my-thing-(stitches-normal-class-name-resolution) { color: blue; }
</style> I'm happy to jump on a call at your all's convenience and demo some of the behavior I've seen with issues noted above as well, I haven't spun up a demo repo yet as at least in my current use-case, I believe it might involve multiple npm package deploys 😅. In any case, I want to reiterate that stitches has been an incredible library to work with 🥳 🙏. I'm confident that the friction points above are something that we can find a tenable, quality solution for 😄. |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 6 replies
-
In my case the hash solution would cover the custom |
Beta Was this translation helpful? Give feedback.
-
I went ahead and put together #536 to ease the consumer friction points noted above 😄 cc: @peduarte , @jonathantneal |
Beta Was this translation helpful? Give feedback.
-
Appears resolved by #546 🥳 😄 |
Beta Was this translation helpful? Give feedback.
-
Hello there, this is a great discussion and sort of touches on the issue that I am having. What if on the library side, we wanted to expose its For example, my host application is a Next.js app that utilizes stitches. In the Now I have this common component that I want on every page that comes from a separate library, like a Navigation component. I want to include it's styles as well in my // _document.js
import { getCssString } from 'stitches-config';
import { getCssString as getSharedLibCssString } from 'my-shared-lib'; // This has a prefix set in its config
// ... removed for brevity
<style id="stitches" dangerouslySetInnerHTML={ { __html: getCssString() + getSharedLibCssString() } } /> This unfortunately does not work. Additional styles are rendered once hydration happens, causing the level of precedence to change as new styles are inserted after the original declarations. Please let me know your thoughts on this aspect. |
Beta Was this translation helpful? Give feedback.
Appears resolved by #546 🥳 😄