feat: add breakpoint simulator for condition-based media queries #5601
+218
−0
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.
Problem
When defining CSS variables with different values for condition-based breakpoints (e.g.,
prefers-color-scheme:darkvsprefers-color-scheme:light), designers had no way to preview these variations without manually changing system preferences.Example:
Previously, testing dark mode required switching OS settings back and forth.
Solution
Implements runtime CSS media rule manipulation to simulate condition-based breakpoints without changing system settings. When a designer selects a condition-based breakpoint, the simulator dynamically modifies CSS media rules:
prefers-color-scheme:darkwhen dark is selected) → replaced with(min-width: 0px)(always true)(max-width: 0px)(always false)Features
Core Functionality:
prefers-color-scheme,prefers-reduced-motion, etc.)Performance:
Robustness:
Technical Implementation
Key Functions:
parseCondition()- Parses condition stringsgetMediaRulesForFeature()- Caches and retrieves relevant media rulesapplySimulation()- Debounced application of media rule modificationsrestoreAllRules()- Restores original media queriessubscribeBreakpointSimulator()- Integrates with nano-states and lifecycleIntegration:
$selectedBreakpointnano-stateTesting Instructions
Setup:
prefers-color-scheme:light,prefers-color-scheme:dark)Test Cases:
Files Changed
apps/builder/app/canvas/shared/breakpoint-simulator.ts- New feature (210 lines)apps/builder/app/canvas/canvas.tsx- IntegrationImpact
Designers can now preview dark/light themes and other condition-based styles instantly without changing system settings, significantly improving the workflow for building modern, accessible interfaces.