Repository navigation
feat: add setAppearance for dark mode and forward custom CSS [ENG-3646] #82
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
Dhruwang
wants to merge
3
commits into
main
Choose a base branch
from
feat/set-appearance-custom-css
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
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
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
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
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
51 changes: 51 additions & 0 deletions
51
packages/react-native/src/components/tests/appearance-sync.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,51 @@ | ||
| import { describe, expect, test } from "vitest"; | ||
| import { createAppearanceSync } from "@/components/utils/appearance-sync"; | ||
|
|
||
| describe("createAppearanceSync", () => { | ||
| test("sends nothing before the survey has rendered", () => { | ||
| const sync = createAppearanceSync(); | ||
| sync.reset("light"); | ||
|
|
||
| expect(sync.onChange("dark")).toBeUndefined(); | ||
| expect(sync.onChange("dark")).toBeUndefined(); | ||
| }); | ||
|
|
||
| /** The bug: a change made while the bundle loads was dropped, and later repeats deduplicated. */ | ||
| test("sends the latest value on render when it changed while loading", () => { | ||
| const sync = createAppearanceSync(); | ||
| sync.reset("light"); | ||
| sync.onChange("dark"); | ||
|
|
||
| expect(sync.onRendered("dark")).toBe("dark"); | ||
| // Recorded as applied, so a repeat is a no-op. | ||
| expect(sync.onChange("dark")).toBeUndefined(); | ||
| }); | ||
|
|
||
| test("sends nothing on render when the value matches the one baked into the HTML", () => { | ||
| const sync = createAppearanceSync(); | ||
| sync.reset("dark"); | ||
|
|
||
| expect(sync.onRendered("dark")).toBeUndefined(); | ||
| }); | ||
|
|
||
| test("sends only real changes once rendered", () => { | ||
| const sync = createAppearanceSync(); | ||
| sync.reset("light"); | ||
| sync.onRendered("light"); | ||
|
|
||
| expect(sync.onChange("light")).toBeUndefined(); | ||
| expect(sync.onChange("dark")).toBe("dark"); | ||
| expect(sync.onChange("dark")).toBeUndefined(); | ||
| expect(sync.onChange("light")).toBe("light"); | ||
| }); | ||
|
|
||
| test("reset for a new survey waits for that survey to render again", () => { | ||
| const sync = createAppearanceSync(); | ||
| sync.reset("light"); | ||
| sync.onRendered("light"); | ||
| sync.reset("dark"); | ||
|
|
||
| expect(sync.onChange("light")).toBeUndefined(); | ||
| expect(sync.onRendered("light")).toBe("light"); | ||
| }); | ||
| }); |
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
43 changes: 43 additions & 0 deletions
43
packages/react-native/src/components/utils/appearance-sync.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,43 @@ | ||
| import type { TResolvedAppearance } from "@/lib/common/appearance"; | ||
|
|
||
| /** | ||
| * Decides when an open survey's appearance has to be sent to the renderer. | ||
| * | ||
| * Until the renderer reports `onSurveyRendered` there is no `formbricksSurveys.setAppearance` to | ||
| * call, so an injection would be a no-op. Changes made meanwhile therefore wait, and the rendered | ||
| * handshake sends the latest one. Each method returns the value to inject, or `undefined` when | ||
| * nothing needs sending; a returned value is recorded as applied. | ||
| */ | ||
| export interface TAppearanceSync { | ||
| /** A survey is shown with `initial` baked into its HTML; it has not rendered yet. */ | ||
| reset: (initial: TResolvedAppearance) => void; | ||
| /** The resolved appearance changed while the survey is shown. */ | ||
| onChange: (resolved: TResolvedAppearance) => TResolvedAppearance | undefined; | ||
| /** The renderer exists; `current` is the appearance resolved right now. */ | ||
| onRendered: (current: TResolvedAppearance) => TResolvedAppearance | undefined; | ||
| } | ||
|
|
||
| export const createAppearanceSync = (): TAppearanceSync => { | ||
| let rendered = false; | ||
| let applied: TResolvedAppearance = "light"; | ||
|
|
||
| const sendIfChanged = ( | ||
| resolved: TResolvedAppearance, | ||
| ): TResolvedAppearance | undefined => { | ||
| if (resolved === applied) return undefined; | ||
| applied = resolved; | ||
| return resolved; | ||
| }; | ||
|
|
||
| return { | ||
| reset: (initial) => { | ||
| rendered = false; | ||
| applied = initial; | ||
| }, | ||
| onChange: (resolved) => (rendered ? sendIfChanged(resolved) : undefined), | ||
| onRendered: (current) => { | ||
| rendered = true; | ||
| return sendIfChanged(current); | ||
| }, | ||
| }; | ||
| }; |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.