-
Notifications
You must be signed in to change notification settings - Fork 4k
Implement host->guest postMessage to update theme details #6393
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
Conversation
…ues (since theme uses px) and keep relative sizes (rem) untouched.
|
Just in case its helpful, worked on a couple potential FE tests -> For import { act } from "react-dom/test-utils"
import FontFaceDeclaration from "./components/core/FontFaceDeclaration"
const mockCustomThemeConfig = {
primaryColor: "#1A6CE7",
backgroundColor: "#FFFFFF",
secondaryBackgroundColor: "#F5F5F5",
textColor: "#1A1D21",
widgetBackgroundColor: "#FFFFFF",
widgetBorderColor: "#D3DAE8",
fontFaces: [
{
family: "Inter",
url: "https://rsms.me/inter/font-files/Inter-Regular.woff2?v=3.19",
weight: 400,
},
],
}
it("handles custom theme sent from Host", () => {
const wrapper = mount(<ThemedApp />)
let fontFaceComponent = wrapper.find(FontFaceDeclaration)
expect(fontFaceComponent.exists()).toBe(false)
const props = wrapper.find(AppWithScreencast).props()
act(() => {
props.theme.setImportedTheme(mockCustomThemeConfig)
})
wrapper.update()
const updatedTheme: ThemeConfig = wrapper.find(AppWithScreencast).props().theme.activeTheme
expect(updatedTheme.name).toBe(CUSTOM_THEME_NAME)
expect(updatedTheme.emotion.genericColors.primary).toBe(mockCustomThemeConfig.primaryColor)
fontFaceComponent = wrapper.find(FontFaceDeclaration)
expect(fontFaceComponent.exists()).toBe(true)
expect(fontFaceComponent.props().fontFaces).toEqual(mockCustomThemeConfig.fontFaces)
})For const mockCustomThemeConfig = {
primaryColor: "#1A6CE7",
backgroundColor: "#FFFFFF",
secondaryBackgroundColor: "#F5F5F5",
textColor: "#1A1D21",
widgetBackgroundColor: "#FFFFFF",
widgetBorderColor: "#D3DAE8",
}
it("can process a received SET_CUSTOM_THEME_CONFIG message", () => {
act(() => {
dispatchEvent(
"message",
new MessageEvent("message", {
data: {
stCommVersion: HOST_COMM_VERSION,
type: "SET_CUSTOM_THEME_CONFIG",
theme: mockCustomThemeConfig,
},
origin: "http://devel.streamlit.test",
})
)
})
wrapper.update()
const theme = wrapper.find(TestComponent).prop("theme")
expect(theme.setImportedTheme).toHaveBeenCalledWith(mockCustomThemeConfig)
}) |
|
Looks like only remaining test failure is in const NEW_SESSION_JSON = {
config: {
gatherUsageStats: false,
maxCachedMessageAge: 0,
mapboxToken: "mapboxToken",
allowRunOnSave: false,
hideSidebarNav: false,
},
customTheme: {
primaryColor: "red",
fontFaces: [],
},
// .... rest of the stuff here |
|
Thanks a lot for looking into this, @mayagbarnes! This test had been fixed on c3e6026. Let me know if you need anything else here! |
frontend/src/hocs/withHostCommunication/withHostCommunication.tsx
Outdated
Show resolved
Hide resolved
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.
Please see comment to fix withHostCommunication/types.ts discrepancy, as well as clarification below on intended behavior, but otherwise LGTM 👍🏼
Will ask Vincent for protobuf codeowner review at standup tomorrow
|
Also when I test locally and hard refresh (browser refresh button vs just hitting R to re-run), the theme reverts back to our standard theme vs restoring the theme sent with postMessage. This is intended behavior/unproblematic for SIS right? |
@sfc-gh-tteixeira or someone on the SiS team might be able to confirm with a bit more detail than me, but my understanding was that the way SiS infrastructure works, the app will be reloaded every time you access it, and we don't have any caching mechanism for themes there, so the message will always be re-sent, causing the app to update the theme accordingly (tough leading to other potential problems, such as flickering when the theme updates). |
The localStorage cache is not required for SiS. It would be great if it worked, but also OK if it doesn't. (And even if it worked, there are questions about when should we clear it. Do we do something smart there today, clearing based on the hash of the config or something?) |
Ah, we do actually reapply the theme when the hash of the theming config received from the server changes (see the |
vdonato
left a comment
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.
Proto changes LGTM
I also did a quick skim of the code itself and it looked fine to me, but I'm mostly relying on @mayagbarnes' review for an actual code review
| string widget_background_color = 7; | ||
| string widget_border_color = 8; | ||
| Radii radii = 9; | ||
| string body_font = 13; | ||
| string code_font = 14; | ||
| repeated FontFace font_faces = 15; | ||
| FontSizes font_sizes = 16; | ||
| } | ||
|
|
||
| message FontFace { | ||
| string url = 1; | ||
| string family = 2; | ||
| int32 weight = 3; | ||
| string style = 4; | ||
| } | ||
|
|
||
| message Radii { | ||
| // In pixels. | ||
| int32 baseWidgetRadius = 1; | ||
| int32 checkboxRadius = 2; | ||
| } | ||
|
|
||
| message FontSizes { | ||
| // In pixels. | ||
| int32 tinyFontSize = 1; | ||
| int32 smallFontSize = 2; | ||
| int32 baseFontSize = 3; |
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.
We'll need to give @sfc-gh-wschmitt a heads up that a bunch of fields are being added here, but I'm assuming this is okay because we probably just nuke the theme custom_theme field entirely when sanitizing forward messages
frontend/src/hocs/withHostCommunication/withHostCommunication.tsx
Outdated
Show resolved
Hide resolved
|
Ok, I'm seeing checkmarks next to both your names in Github, and looking at the discussion it feels like everything is resolved, so I'll just go ahead and merge. Some future work:
|
* develop: Implement host->guest postMessage to update theme details (#6393)
| headingFont: parsedFont, | ||
| bodyFont: themeInput.bodyFont ? themeInput.bodyFont : parsedFont, | ||
| headingFont: themeInput.bodyFont ? themeInput.bodyFont : parsedFont, | ||
| codeFont: themeInput.codeFont ? themeInput.codeFont : parsedFont, |
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.
@sfc-gh-tteixeira I am not sure if we want the same font in 3 places. This probably caused the regressions. See:
#6484
📚 Context
When iframing Streamlit, this PR makes it possible for the host frame to update Streamlit's theme in some detail.
Note that this is not meant to be a public API (yet??)
What this PR does
properties are:
used in the sidebar.
Snowsight's theme, and is a barely noticeable change when using Streamlit's.
are called basewidgetradius and checkboxradius in the postmessage.
What's missing:
What kind of change does this PR introduce?
🧠 Description of Changes
Add bullet points summarizing your changes here
Revised:
Insert screenshot of your updated UI/code here
Current:
Insert screenshot of existing UI/code here
🧪 Testing Done
🌐 References
Does this depend on other work, documents, or tickets?
Contribution License Agreement
By submitting this pull request you agree that all contributions to this project are made under the Apache 2.0 license.