bos-web-engine icon indicating copy to clipboard operation
bos-web-engine copied to clipboard

🔷 [Epic] CSS-in-JS support

Open mpeterdev opened this issue 2 years ago • 6 comments

To allow developers to style their BOS components without inlining all styles on each React component, we should support a CSS-in-JS solution

styled-components is the initial target since it is already used in the VM and is one of the most highly adopted CSS-in-JS tools

Implementation notes

We need a way to send the style data to the Outer Window Application where they can be applied to the Widget DOM. It looks like integrating styled-components into the Outer Window Application is the approach to try first. The hard part will be mapping the styled component to the serialized component.

mpeterdev avatar Aug 10 '23 17:08 mpeterdev

styles is shimmed in the current implementation to pass back the HTML element type, ignoring the CSS in the template string: https://github.com/near/bos-web-engine/blob/main/packages/iframe/src/SandboxedIframe.tsx#L164.

If this CSS string can be encoded into the serialized DOM node, then there should be enough context to incorporate the CSS into the deserialized node rendered in the Outer Web Application.

e.g. a naive implementation of this could be simply concatenating the CSS onto the HTML type:

          const styled = new Proxy({}, {
            get(target, property, receiver) {
              return (css) => {
                return property + ':' + css;
              };
            }
          });

Where the React node deserialization would split the CSS out and interpret it as the element's inline style.

Since the Outer Web Application is building raw React with createElement, we will likely require some interpretation of the CSS such that it is inline with the currently supported styled-components approach. In particular I am not sure how more advanced use cases like media queries will be supported.

andy-haynes avatar Sep 08 '23 18:09 andy-haynes

@andy-haynes should this come after #57 since styled-components leverages a plugin from either babel or swc?

mpeterdev avatar Nov 01 '23 19:11 mpeterdev

relevant: https://styled-components.com/docs/basics#define-styled-components-outside-of-the-render-method

mpeterdev avatar Nov 01 '23 19:11 mpeterdev

If SWC and Babel each only require a single plugin then I'd consider it a loose dependency. It doesn't look like we'd spend much time on a transpiler integration either way so I don't necessarily see a problem with prioritizing this over SWC if we want CSS sooner.

andy-haynes avatar Nov 01 '23 19:11 andy-haynes

https://github.com/near/bos-web-engine/discussions/130#discussioncomment-7459805

mpeterdev avatar Nov 02 '23 18:11 mpeterdev

if this proves challenging, especially to support features like media queries, we could downgrade the priority of this epic to P1/P2 and upgrade #8 to a P0

mpeterdev avatar Nov 08 '23 19:11 mpeterdev