-
Notifications
You must be signed in to change notification settings - Fork 5k
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
Changes from 1 commit
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
6ab10e3
apply css variables injection right under the public component
heypoom 5cda8d7
refactor css variables
heypoom d7141df
fix name changes
heypoom b50f950
apply css variables to global styles
heypoom a95038d
update path
heypoom ed66c37
render component wrapper as full-width
heypoom 3fbef20
Merge remote-tracking branch 'origin/master' into fix-missing-css-var…
heypoom f4dfc31
move style resets to public component styles wrapper
heypoom d79a67f
update unit tests for global styles
heypoom File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
87 changes: 87 additions & 0 deletions
87
enterprise/frontend/src/embedding-sdk/components/private/PublicComponentStylesWrapper.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
import styled from "@emotion/styled"; | ||
import type { ReactNode } from "react"; | ||
|
||
import { color } from "metabase/lib/colors"; | ||
import { useThemeSpecificCssVariables } from "metabase/styled-components/theme/theme"; | ||
|
||
/** | ||
* Injects CSS variables and styles to the SDK components underneath them. | ||
* This is to ensure that the SDK components are styled correctly, | ||
* even when rendered under a React portal. | ||
*/ | ||
export function PublicComponentStylesWrapper({ | ||
children, | ||
}: { | ||
children: ReactNode; | ||
}) { | ||
const themeSpecificCssVariables = useThemeSpecificCssVariables(); | ||
|
||
return ( | ||
<StylesWrapperInner themeSpecificCssVariables={themeSpecificCssVariables}> | ||
{children} | ||
</StylesWrapperInner> | ||
); | ||
} | ||
|
||
const StylesWrapperInner = styled.div<{ | ||
themeSpecificCssVariables: string; | ||
}>` | ||
/* NOTE: DO NOT ADD COLORS WITHOUT EXTREMELY GOOD REASON AND DESIGN REVIEW | ||
* NOTE: KEEP SYNCHRONIZED WITH: | ||
* frontend/src/metabase/ui/utils/colors.ts | ||
* frontend/src/metabase/styled-components/containers/GlobalStyles/GlobalStyles.tsx | ||
* frontend/src/metabase/css/core/colors.module.css | ||
* .storybook/preview-head.html | ||
*/ | ||
--mb-default-font-family: "${({ theme }) => theme.fontFamily}"; | ||
--mb-color-bg-light: ${({ theme }) => theme.fn.themeColor("bg-light")}; | ||
--mb-color-bg-dark: ${({ theme }) => theme.fn.themeColor("bg-dark")}; | ||
--mb-color-brand: ${({ theme }) => theme.fn.themeColor("brand")}; | ||
|
||
--mb-color-brand-light: color-mix(in srgb, var(--mb-color-brand) 53%, #fff); | ||
--mb-color-brand-lighter: color-mix(in srgb, var(--mb-color-brand) 60%, #fff); | ||
--mb-color-brand-alpha-04: color-mix( | ||
in srgb, | ||
var(--mb-color-brand) 4%, | ||
transparent | ||
); | ||
--mb-color-brand-alpha-88: color-mix( | ||
in srgb, | ||
var(--mb-color-brand) 88%, | ||
transparent | ||
); | ||
|
||
--mb-color-focus: ${({ theme }) => theme.fn.themeColor("focus")}; | ||
--mb-color-bg-white: ${({ theme }) => theme.fn.themeColor("bg-white")}; | ||
--mb-color-bg-black: ${({ theme }) => theme.fn.themeColor("bg-black")}; | ||
--mb-color-shadow: ${({ theme }) => theme.fn.themeColor("shadow")}; | ||
--mb-color-border: ${({ theme }) => theme.fn.themeColor("border")}; | ||
--mb-color-text-dark: ${({ theme }) => theme.fn.themeColor("text-dark")}; | ||
--mb-color-text-medium: ${({ theme }) => theme.fn.themeColor("text-medium")}; | ||
--mb-color-text-light: ${({ theme }) => theme.fn.themeColor("text-light")}; | ||
--mb-color-danger: ${({ theme }) => theme.fn.themeColor("danger")}; | ||
--mb-color-error: ${({ theme }) => theme.fn.themeColor("error")}; | ||
--mb-color-filter: ${({ theme }) => theme.fn.themeColor("filter")}; | ||
--mb-color-bg-error: ${() => color("bg-error")}; | ||
--mb-color-bg-medium: ${({ theme }) => theme.fn.themeColor("bg-medium")}; | ||
--mb-color-bg-night: ${() => color("bg-night")}; | ||
--mb-color-text-white: ${({ theme }) => theme.fn.themeColor("text-white")}; | ||
--mb-color-success: ${({ theme }) => theme.fn.themeColor("success")}; | ||
--mb-color-summarize: ${({ theme }) => theme.fn.themeColor("summarize")}; | ||
--mb-color-warning: ${({ theme }) => theme.fn.themeColor("warning")}; | ||
|
||
/** | ||
Theming-specific CSS variables. | ||
Keep in sync with [GlobalStyles.tsx] and [.storybook/preview-head.html]. | ||
|
||
Refer to DEFAULT_METABASE_COMPONENT_THEME for their defaults. | ||
|
||
These CSS variables are not part of the core design system colors. | ||
Do NOT add them to [palette.ts] and [colors.ts]. | ||
*/ | ||
${({ themeSpecificCssVariables }) => themeSpecificCssVariables} | ||
|
||
:where(svg) { | ||
display: inline; | ||
} | ||
`; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
106 changes: 0 additions & 106 deletions
106
enterprise/frontend/src/embedding-sdk/components/private/SdkContentWrapper.tsx
This file was deleted.
Oops, something went wrong.
57 changes: 57 additions & 0 deletions
57
enterprise/frontend/src/embedding-sdk/components/private/SdkGlobalStylesWrapper.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import { css } from "@emotion/react"; | ||
import styled from "@emotion/styled"; | ||
import type { HTMLAttributes } from "react"; | ||
|
||
import { rootStyle } from "metabase/css/core/base.styled"; | ||
import { defaultFontFiles } from "metabase/css/core/fonts.styled"; | ||
import { useSelector } from "metabase/lib/redux"; | ||
import { aceEditorStyles } from "metabase/query_builder/components/NativeQueryEditor/NativeQueryEditor.styled"; | ||
import { getFontFiles } from "metabase/styled-components/selectors"; | ||
import { saveDomImageStyles } from "metabase/visualizations/lib/save-chart-image"; | ||
import type { FontFile } from "metabase-types/api"; | ||
|
||
interface SdkContentWrapperProps { | ||
baseUrl?: string; | ||
} | ||
|
||
export function SdkGlobalStylesWrapper({ | ||
baseUrl, | ||
...divProps | ||
}: SdkContentWrapperProps & HTMLAttributes<HTMLDivElement>) { | ||
const fontFiles = useSelector(getFontFiles); | ||
|
||
return ( | ||
<SdkGlobalStylesInner | ||
baseUrl={baseUrl} | ||
fontFiles={fontFiles} | ||
{...divProps} | ||
/> | ||
); | ||
} | ||
|
||
const SdkGlobalStylesInner = styled.div< | ||
SdkContentWrapperProps & { | ||
fontFiles: FontFile[] | null; | ||
} | ||
>` | ||
font-size: ${({ theme }) => theme.other.fontSize}; | ||
|
||
${aceEditorStyles} | ||
${saveDomImageStyles} | ||
${rootStyle} | ||
|
||
${({ baseUrl }) => defaultFontFiles({ baseUrl })} | ||
|
||
${({ fontFiles }) => | ||
fontFiles?.map( | ||
file => css` | ||
@font-face { | ||
font-family: "Custom"; | ||
src: url(${encodeURI(file.src)}) format("${file.fontFormat}"); | ||
font-weight: ${file.fontWeight}; | ||
font-style: normal; | ||
font-display: swap; | ||
} | ||
Comment on lines
+47
to
+53
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
`, | ||
)} | ||
`; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
andsaveDomImageStyles
here, as well as addingcolor
andfont-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).