-
Notifications
You must be signed in to change notification settings - Fork 16
fix(KNO-14012): inject guide toolbar styles at runtime #1043
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
Open
thomaswhyyou
wants to merge
1
commit into
main
Choose a base branch
from
thomas-kno-14012-investigate-bundling-css-import-into-toolbar
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
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 hidden or 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,7 @@ | ||
| --- | ||
| "@knocklabs/react": patch | ||
| --- | ||
|
|
||
| fix(KNO-14012): guide toolbar no longer requires importing `@knocklabs/react/dist/index.css` | ||
|
|
||
| The guide toolbar previously rendered unstyled (or with frankenstein styling) unless the consuming app imported `@knocklabs/react/dist/index.css`. Apps that only render custom guide components have no reason to import that stylesheet, so the toolbar broke for them. The toolbar now injects its compiled styles into the document head at runtime when it becomes visible, making it plug and play without an explicit CSS import. When `dist/index.css` is imported anyway, the injected rules are identical duplicates and have no effect. |
This file contains hidden or 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
21 changes: 21 additions & 0 deletions
21
packages/react/src/modules/guide/components/Toolbar/useToolbarStyles.ts
This file contains hidden or 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,21 @@ | ||
| import React from "react"; | ||
|
|
||
| import toolbarStyles from "./styles.css?inline"; | ||
|
|
||
| const STYLE_ELEMENT_ID = "knock-guide-toolbar-styles"; | ||
|
|
||
| // The toolbar's styles are bundled into dist/index.css, but consumers who only | ||
| // render custom guide components have no reason to import that stylesheet. | ||
| // Inject the toolbar's compiled styles at runtime when it becomes visible, so | ||
| // the toolbar works without an explicit CSS import. When dist/index.css is | ||
| // imported anyway, the injected rules are identical duplicates and harmless. | ||
| export const useToolbarStyles = (enabled: boolean) => { | ||
| React.useEffect(() => { | ||
| if (!enabled || document.getElementById(STYLE_ELEMENT_ID)) return; | ||
|
|
||
| const styleElement = document.createElement("style"); | ||
| styleElement.id = STYLE_ELEMENT_ID; | ||
| styleElement.textContent = toolbarStyles; | ||
| document.head.appendChild(styleElement); | ||
| }, [enabled]); | ||
| }; | ||
This file contains hidden or 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,56 @@ | ||
| import { render } from "@testing-library/react"; | ||
| import { afterEach, describe, expect, test, vi } from "vitest"; | ||
|
|
||
| import { V2 } from "../../../../src/modules/guide/components/Toolbar/V2/V2"; | ||
|
|
||
| vi.mock("@knocklabs/react-core", async () => { | ||
| const actual = await vi.importActual("@knocklabs/react-core"); | ||
| return { | ||
| ...actual, | ||
| useGuideContext: () => ({ | ||
| client: { | ||
| store: { state: {} }, | ||
| setDebug: vi.fn(), | ||
| unsetDebug: vi.fn(), | ||
| }, | ||
| }), | ||
| }; | ||
| }); | ||
|
|
||
| vi.mock( | ||
| "../../../../src/modules/guide/components/Toolbar/V2/useInspectGuideClientStore", | ||
| async () => { | ||
| const actual = await vi.importActual( | ||
| "../../../../src/modules/guide/components/Toolbar/V2/useInspectGuideClientStore", | ||
| ); | ||
| return { | ||
| ...actual, | ||
| useInspectGuideClientStore: () => null, | ||
| }; | ||
| }, | ||
| ); | ||
|
|
||
| const getStyleElement = () => | ||
| document.getElementById("knock-guide-toolbar-styles"); | ||
|
|
||
| afterEach(() => { | ||
| getStyleElement()?.remove(); | ||
| window.history.replaceState({}, "", "/"); | ||
| }); | ||
|
|
||
| describe("Toolbar V2", () => { | ||
| test("injects toolbar styles when launched via the toolbar url param", () => { | ||
| window.history.replaceState({}, "", "/?knock_guide_toolbar=true"); | ||
|
|
||
| render(<V2 readyToTarget={false} listenForUpdates={false} />); | ||
|
|
||
| expect(getStyleElement()).not.toBeNull(); | ||
| expect(getStyleElement()?.parentElement).toBe(document.head); | ||
| }); | ||
|
|
||
| test("does not inject toolbar styles without the toolbar url param", () => { | ||
| render(<V2 readyToTarget={false} listenForUpdates={false} />); | ||
|
|
||
| expect(getStyleElement()).toBeNull(); | ||
| }); | ||
| }); |
44 changes: 44 additions & 0 deletions
44
packages/react/test/guide/Toolbar/useToolbarStyles.test.ts
This file contains hidden or 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,44 @@ | ||
| import { renderHook } from "@testing-library/react"; | ||
| import { afterEach, describe, expect, test } from "vitest"; | ||
|
|
||
| import { useToolbarStyles } from "../../../src/modules/guide/components/Toolbar/useToolbarStyles"; | ||
|
|
||
| const getStyleElement = () => | ||
| document.getElementById("knock-guide-toolbar-styles"); | ||
|
|
||
| afterEach(() => { | ||
| getStyleElement()?.remove(); | ||
| }); | ||
|
|
||
| describe("useToolbarStyles", () => { | ||
| test("does not inject styles when disabled", () => { | ||
| renderHook(() => useToolbarStyles(false)); | ||
| expect(getStyleElement()).toBeNull(); | ||
| }); | ||
|
|
||
| test("injects a style element into the document head when enabled", () => { | ||
| renderHook(() => useToolbarStyles(true)); | ||
| const styleElement = getStyleElement(); | ||
| expect(styleElement).not.toBeNull(); | ||
| expect(styleElement?.parentElement).toBe(document.head); | ||
| }); | ||
|
|
||
| test("injects styles once it becomes enabled", () => { | ||
| const { rerender } = renderHook( | ||
| ({ enabled }) => useToolbarStyles(enabled), | ||
| { initialProps: { enabled: false } }, | ||
| ); | ||
| expect(getStyleElement()).toBeNull(); | ||
|
|
||
| rerender({ enabled: true }); | ||
| expect(getStyleElement()).not.toBeNull(); | ||
| }); | ||
|
|
||
| test("injects only one style element across multiple instances", () => { | ||
| renderHook(() => useToolbarStyles(true)); | ||
| renderHook(() => useToolbarStyles(true)); | ||
| expect( | ||
| document.querySelectorAll("#knock-guide-toolbar-styles"), | ||
| ).toHaveLength(1); | ||
| }); | ||
| }); |
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.
Inline CSS stripped by build
High Severity
useToolbarStylesimports toolbar CSS viastyles.css?inline, but the React package build deletes all*.css.js/*.css.mjsartifacts and strips CSSrequires. Withvite-plugin-no-bundle(preserveModules), that inline module is emitted as one of those files, so the published package loses the style string and runtime injection cannot work.Reviewed by Cursor Bugbot for commit b1a58af. Configure 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.
@thomaswhyyou this one sounds legit yeah?