Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(sdk): missing css variables when rendering under a portal #44473

Merged
merged 9 commits into from
Jun 25, 2024

Conversation

heypoom
Copy link
Contributor

@heypoom heypoom commented Jun 20, 2024

Closes #44391

Description

When SDK users are rendering the embedding SDK components (such as an interactive question) under a React portal (such as in a modal using their library of choice), the filter colors from the theme settings are not applied. In the screenshot below, you can notice that the filter color is not applied properly in (1).

image

This PR separates the components for injecting CSS variables to a public component (i.e. PublicComponentStylesWrapper) from the components injecting global sdk styles such as font-face declarations (i.e. SdkGlobalStylesWrapper), so we can provide the CSS variables in context of React portals.

How to verify

  • Go to the fix-react-portal branch under the Shoppy demo app
  • Click on a product card, then click on the topmost chart
  • The color should be correct

Demo

CleanShot 2567-06-20 at 21 12 11@2x

Checklist

  • Tests have been added/updated to cover changes in this PR - to add visual test or unit test next

Copy link

github-actions bot commented Jun 20, 2024

Codenotify: Notifying subscribers in CODENOTIFY files for diff 105a72a...d79a67f.

Notify File(s)
@ranquild frontend/src/metabase/lib/colors/palette.ts

@heypoom heypoom added the no-backport Do not backport this PR to any branch label Jun 20, 2024
Copy link

replay-io bot commented Jun 20, 2024

Status Complete ↗︎
Commit d79a67f
Results
⚠️ 2 Flaky
2689 Passed

Comment on lines +48 to +54
@font-face {
font-family: "Custom";
src: url(${encodeURI(file.src)}) format("${file.fontFormat}");
font-weight: ${file.fontWeight};
font-style: normal;
font-display: swap;
}
Copy link
Contributor Author

@heypoom heypoom Jun 20, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@font-face declarations are always global in nature. It is not scoped. We don't need to add them to PublicComponentWrapper

fontFiles: FontFile[] | null;
}
>`
${({ theme }) => getMetabaseCssVariables(theme)}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need this because of components like Popovers. Popovers still need the Metabase CSS variables for their colors, so we inject the CSS variables in both SdkGlobalStyles (applies to Mantine components) and PublicComponentStylesWrapper (applies to Metabase components). Refer to this thread for more info.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is what happens when we only inject the CSS variables in the component wrapper. It breaks quite a lot of places.

CleanShot 2567-06-20 at 20 55 27@2x

Comment on lines 4 to +5
* frontend/src/metabase/styled-components/containers/GlobalStyles/GlobalStyles.tsx
* enterprise/frontend/src/embedding-sdk/components/private/SdkContentWrapper.tsx
* frontend/src/metabase/styled-components/theme/css-variables.ts
Copy link
Contributor Author

@heypoom heypoom Jun 20, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I want to remove the CSS variables from GlobalStyles and rely on getMetabaseCssVariables, but that's more testing surface so let's do that in the next PR where I can focus on testing. If we do that, there will be a lot less duplication between GlobalStyles and SDK.

@heypoom heypoom requested a review from a team June 20, 2024 14:12
@heypoom
Copy link
Contributor Author

heypoom commented Jun 20, 2024

Note: I'll add visual test or unit test in this PR. Adding reviewers for now to see if this makes sense to other folks.

width: 100%;
${({ theme }) => getMetabaseCssVariables(theme)}

:where(svg) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall I like the approach, but we need to guarantee that other global styles which are needed will be also copied here. I think we should add a common css block that contains such common styles

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@deniskaber good idea! i'll take a look at the CSS to see if there are more styles we can extract to here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@deniskaber I re-checked the CSS, and I've moved aceEditorStyles and saveDomImageStyles here, as well as adding color and font-family that matches GlobalStyles.

The only thing to be left in SdkGlobalStyles would then be font-size (cannot be in PublicComponentStyles due to cascades), rootStyle and font file loading (@font-face is global).

@deniskaber deniskaber requested a review from a team June 21, 2024 20:17
@heypoom heypoom marked this pull request as draft June 24, 2024 11:53
@heypoom heypoom removed the request for review from a team June 24, 2024 14:18
@heypoom heypoom marked this pull request as ready for review June 24, 2024 19:26
@heypoom heypoom requested a review from a team June 24, 2024 19:32
@heypoom heypoom merged commit 70eea11 into master Jun 25, 2024
112 checks passed
@heypoom heypoom deleted the fix-missing-css-variables-when-under-portal branch June 25, 2024 05:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
no-backport Do not backport this PR to any branch .Team/Embedding
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Embedding SDK components has missing CSS variables when rendering in a React portal
2 participants