Repository navigation
feat: add setAppearance for dark mode and forward custom CSS [ENG-3647] #61
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
4
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
4 commits
Select commit
Hold shift + click to select a range
6d1da2b
feat: add setAppearance for dark mode and forward custom CSS to the s…
Dhruwang 0bf335d
fix: format, cover the appearance paths, notify the scope after the f…
Dhruwang dac8eb1
fix: send appearance changes made while the survey loads [ENG-3647]
itsjavi 93b0ac1
test: cover the baked-in appearance, the rendered event and the platf…
Dhruwang 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
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,148 @@ | ||
| /// How surveys render, and the in-memory state behind `Formbricks.setAppearance`. | ||
| library; | ||
|
|
||
| import 'package:flutter/cupertino.dart'; | ||
| import 'package:flutter/material.dart' show Theme; | ||
|
|
||
| import 'logger.dart'; | ||
|
|
||
| /// How surveys render. [system] follows the host app's own theme — its | ||
| /// `Theme` / `ThemeMode` — not the phone's. | ||
| enum FormbricksAppearance { | ||
| /// Always light (the default). | ||
| light, | ||
|
|
||
| /// Always dark. | ||
| dark, | ||
|
|
||
| /// Follows the app's theme, live. | ||
| system; | ||
|
|
||
| /// Parses `'light'`, `'dark'` or `'system'`; null for anything else. | ||
| static FormbricksAppearance? tryParse(Object? value) { | ||
| if (value is FormbricksAppearance) return value; | ||
| if (value is! String) return null; | ||
| for (final appearance in values) { | ||
| if (appearance.name == value.trim().toLowerCase()) return appearance; | ||
| } | ||
| return null; | ||
| } | ||
| } | ||
|
|
||
| /// The in-memory appearance state (ENG-3452). | ||
| /// | ||
| /// Never persisted, never sent to the server, and left alone by `logout()`, so a | ||
| /// fresh app launch starts light (ENG-3551). Not routed through the command | ||
| /// queue, so it works before `setup()` completes. | ||
| class AppearanceState extends ChangeNotifier { | ||
| AppearanceState._(); | ||
|
|
||
| /// The process-wide state. | ||
| static final AppearanceState instance = AppearanceState._(); | ||
|
|
||
| FormbricksAppearance _current = FormbricksAppearance.light; | ||
|
|
||
| /// The requested appearance. May be [FormbricksAppearance.system]. | ||
| FormbricksAppearance get current => _current; | ||
|
|
||
| /// Sets the appearance. An unknown value is logged and falls back to light. | ||
| void set(Object? value) { | ||
| final parsed = FormbricksAppearance.tryParse(value); | ||
| if (parsed == null) { | ||
| Logger.error( | ||
| 'setAppearance: unknown appearance "$value", falling back to light', | ||
| ); | ||
| } | ||
| _current = parsed ?? FormbricksAppearance.light; | ||
| notifyListeners(); | ||
| } | ||
|
|
||
| /// Test-only: back to the cold-start state. | ||
| void reset() { | ||
| _current = FormbricksAppearance.light; | ||
| } | ||
| } | ||
|
|
||
| /// What the renderer understands: always `'light'` or `'dark'`, never `'system'`. | ||
| /// | ||
| /// `system` reads the theme of [context], which is the app's own, and only falls | ||
| /// back to the platform brightness when the app sets none. | ||
| String resolveAppearance(BuildContext context, FormbricksAppearance requested) { | ||
| switch (requested) { | ||
| case FormbricksAppearance.light: | ||
| return 'light'; | ||
| case FormbricksAppearance.dark: | ||
| return 'dark'; | ||
| case FormbricksAppearance.system: | ||
| // A Material app (or any `Theme`) states its brightness there; a | ||
| // `Theme.of` with no ancestor would quietly answer light, so look first. | ||
| // Otherwise the Cupertino theme, then the platform as a last resort. | ||
| final brightness = context.findAncestorWidgetOfExactType<Theme>() != null | ||
| ? Theme.of(context).brightness | ||
| : CupertinoTheme.maybeBrightnessOf(context) ?? | ||
| MediaQuery.maybePlatformBrightnessOf(context) ?? | ||
| Brightness.light; | ||
| return brightness == Brightness.dark ? 'dark' : 'light'; | ||
| } | ||
| } | ||
|
|
||
| /// JavaScript that flips an open survey in place. Optional chaining: a server | ||
| /// whose renderer predates `setAppearance` leaves the survey light instead of | ||
| /// throwing. | ||
| String appearanceSwitchScript(String resolved) => | ||
| "window.formbricksSurveys?.setAppearance?.('$resolved');"; | ||
|
|
||
| /// Builds the renderer's `customCss` prop from the workspace and survey CSS. | ||
| /// | ||
| /// The compiled strings pass through untouched. Empty fields are omitted rather | ||
| /// than sent as null — the renderer rejects the whole prop on a null inside a | ||
| /// scope — and null is returned when there is no CSS at all, so the key is never | ||
| /// sent. | ||
| Map<String, Map<String, String>>? buildCustomCss( | ||
| Object? workspace, | ||
| Object? survey, | ||
| ) { | ||
| Map<String, String>? scope(Object? raw) { | ||
| if (raw is! Map) return null; | ||
| final result = <String, String>{}; | ||
| for (final mode in const ['light', 'dark']) { | ||
| final css = raw[mode]; | ||
| if (css is String && css.isNotEmpty) result[mode] = css; | ||
| } | ||
| return result.isEmpty ? null : result; | ||
| } | ||
|
|
||
| final workspaceScope = scope(workspace); | ||
| final surveyScope = scope(survey); | ||
| if (workspaceScope == null && surveyScope == null) return null; | ||
| return { | ||
| if (workspaceScope != null) 'workspace': workspaceScope, | ||
| if (surveyScope != null) 'survey': surveyScope, | ||
| }; | ||
| } | ||
|
|
||
| /// Hands the open survey's resolved appearance to the WebView host. | ||
| /// | ||
| /// An [InheritedNotifier] rather than a new parameter on the host builder, so | ||
| /// injected test hosts keep compiling. The default host listens and runs | ||
| /// [appearanceSwitchScript] when it changes. | ||
| class AppearanceScope extends InheritedNotifier<ValueNotifier<String>> { | ||
| /// Creates a scope around [child]. | ||
| const AppearanceScope({ | ||
| super.key, | ||
| required ValueNotifier<String> appearance, | ||
| this.initial, | ||
| required super.child, | ||
| }) : super(notifier: appearance); | ||
|
|
||
| /// The value baked into the survey page when it was built. | ||
| final String? initial; | ||
|
|
||
| /// The nearest resolved-appearance notifier, if any. | ||
| static ValueNotifier<String>? maybeOf(BuildContext context) => | ||
| context.getInheritedWidgetOfExactType<AppearanceScope>()?.notifier; | ||
|
|
||
| /// The value the page was built with, if any. | ||
| static String? initialOf(BuildContext context) => | ||
| context.getInheritedWidgetOfExactType<AppearanceScope>()?.initial; | ||
| } |
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
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.