-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
feat: Prioritize pseudoselectors over renders #9688
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
wisniewskij
wants to merge
6
commits into
main
Choose a base branch
from
@wisniewskij/pseudoselectors-priority-over-rerender
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
6 commits
Select commit
Hold shift + click to select a range
794985e
initial stuff
wisniewskij f7721a9
Dont detach on render
wisniewskij fdaa473
Add example
wisniewskij 241a9e8
Refine example
wisniewskij b142cf1
Move showcase to a separate tab
wisniewskij a1ab9f5
chore: Planet example for pseudoselectors (#9691)
wisniewskij 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
80 changes: 80 additions & 0 deletions
80
...mmon-app/src/apps/css/examples/transitions/screens/pseudoSelectors/ActiveBlocksRender.tsx
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,80 @@ | ||
| import { useEffect, useState } from 'react'; | ||
| import { StyleSheet } from 'react-native'; | ||
| import Animated from 'react-native-reanimated'; | ||
|
|
||
| import { | ||
| Screen, | ||
| Scroll, | ||
| Section, | ||
| VerticalExampleCard, | ||
| } from '@/apps/css/components'; | ||
| import { colors, radius, sizes, spacing } from '@/theme'; | ||
|
|
||
| export default function ActiveBlocksRender() { | ||
| const [phase, setPhase] = useState(0); | ||
|
|
||
| useEffect(() => { | ||
| const interval = setInterval(() => { | ||
| setPhase((prev) => (prev + 1) % 2); | ||
| }, 1000); | ||
| return () => clearInterval(interval); | ||
| }, []); | ||
|
|
||
| const renderColor = phase === 0 ? colors.danger : colors.primaryLight; | ||
|
|
||
| return ( | ||
| <Screen> | ||
| <Scroll contentContainerStyle={styles.content} withBottomBarSpacing> | ||
| <Section | ||
| description="backgroundColor is driven by two sources at once: a render-driven transition that toggles red <-> light blue every 1000ms via the 'default' value, and a ':active' selector that sets dark navy. Press and hold the box: while ':active' is active, the incoming render transitions on backgroundColor should be blocked and the box should hold dark navy (no red/blue flicker). Release and the looping render transition resumes." | ||
| title="Selector blocks render transition"> | ||
| <VerticalExampleCard | ||
| title="backgroundColor: render loop vs :active" | ||
| code={`const renderColor = phase === 0 ? colors.danger : colors.primaryLight; | ||
|
|
||
| <Animated.View | ||
| style={{ | ||
| backgroundColor: { | ||
| default: renderColor, | ||
| ':active': colors.primaryDark, | ||
| }, | ||
| transitionDuration: '900ms', | ||
| transitionTimingFunction: 'linear', | ||
| }} | ||
| onStartShouldSetResponder={() => true} | ||
| />`} | ||
| collapsedCode={`backgroundColor: { | ||
| default: renderColor, | ||
| ':active': colors.primaryDark, | ||
| },`}> | ||
| <Animated.View | ||
| style={[ | ||
| styles.box, | ||
| { | ||
| backgroundColor: { | ||
| ':active': colors.primaryDark, | ||
| default: renderColor, | ||
| }, | ||
| transitionDuration: '900ms', | ||
| transitionTimingFunction: 'linear', | ||
| }, | ||
| ]} | ||
| onStartShouldSetResponder={() => true} | ||
| /> | ||
| </VerticalExampleCard> | ||
| </Section> | ||
| </Scroll> | ||
| </Screen> | ||
| ); | ||
| } | ||
|
|
||
| const styles = StyleSheet.create({ | ||
| box: { | ||
| borderRadius: radius.md, | ||
| height: sizes.md, | ||
| width: sizes.md, | ||
| }, | ||
| content: { | ||
| gap: spacing.xs, | ||
| }, | ||
| }); | ||
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
151 changes: 151 additions & 0 deletions
151
apps/common-app/src/apps/css/examples/transitions/screens/pseudoSelectors/Planets.tsx
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,151 @@ | ||
| import { StyleSheet, Text } from 'react-native'; | ||
| import Animated, { css } from 'react-native-reanimated'; | ||
|
|
||
| import { | ||
| Screen, | ||
| Scroll, | ||
| Section, | ||
| VerticalExampleCard, | ||
| } from '@/apps/css/components'; | ||
| import { colors, spacing } from '@/theme'; | ||
|
|
||
| const ORBIT_RX = 90; | ||
| const ORBIT_RY = 40; | ||
| const ORBIT_DURATION = '12s'; | ||
| const ORBIT_STEPS = 36; | ||
|
|
||
| const PLANETS = [ | ||
| { color: colors.danger, name: 'Mars', size: 44 }, | ||
| { color: colors.primary, name: 'Venus', size: 60 }, | ||
| { color: colors.primaryDark, name: 'Jupiter', size: 78 }, | ||
| ]; | ||
|
|
||
| const MAX_PLANET_SIZE = Math.max(...PLANETS.map((planet) => planet.size)); | ||
| const PLANE_WIDTH = 2 * ORBIT_RX + MAX_PLANET_SIZE; | ||
| const PLANE_HEIGHT = 2 * ORBIT_RY + MAX_PLANET_SIZE; | ||
| const CENTER_X = PLANE_WIDTH / 2; | ||
| const CENTER_Y = PLANE_HEIGHT / 2; | ||
|
|
||
| function makeOrbit(offsetDeg: number, size: number) { | ||
| const frames: Record<string, { left: number; top: number }> = {}; | ||
| for (let step = 0; step <= ORBIT_STEPS; step++) { | ||
| const percent = ((step / ORBIT_STEPS) * 100).toFixed(4); | ||
| const angle = ((offsetDeg + (360 * step) / ORBIT_STEPS) * Math.PI) / 180; | ||
| frames[`${percent}%`] = { | ||
| left: CENTER_X + ORBIT_RX * Math.cos(angle) - size / 2, | ||
| top: CENTER_Y + ORBIT_RY * Math.sin(angle) - size / 2, | ||
| }; | ||
| } | ||
| return css.keyframes(frames); | ||
| } | ||
|
|
||
| const ORBITS = PLANETS.map((planet, index) => | ||
| makeOrbit((360 / PLANETS.length) * index, planet.size) | ||
| ); | ||
|
|
||
| export default function Planets() { | ||
| return ( | ||
| <Screen> | ||
| <Scroll contentContainerStyle={styles.content} withBottomBarSpacing> | ||
| <Section | ||
| description="Three differently sized planets orbit the screen center on a tilted (elliptical) path. Each planet's name is hidden until you press it, which activates its ':active' pseudo-selector; hovering enlarges it with a colored glow." | ||
| title="Planets"> | ||
| <VerticalExampleCard | ||
| title="Press a planet to reveal its name" | ||
| code={`<Animated.View | ||
| style={{ | ||
| // 0.02 not 0: iOS skips alpha<0.01 views in hit-testing | ||
| opacity: { default: 0.02, ':active': 1 }, | ||
| transitionDuration: '200ms', | ||
| }} | ||
| onStartShouldSetResponder={() => true}> | ||
| <Text>{name}</Text> | ||
| </Animated.View>`} | ||
| collapsedCode={`opacity: { | ||
| default: 0.02, | ||
| ':active': 1, | ||
| },`}> | ||
| <Animated.View style={styles.stage}> | ||
| <Animated.View style={styles.orbitPlane}> | ||
| {PLANETS.map((planet, index) => ( | ||
| <Animated.View | ||
| key={planet.name} | ||
| style={[ | ||
| styles.orbiter, | ||
| { | ||
| animationDuration: ORBIT_DURATION, | ||
| animationIterationCount: 'infinite', | ||
| animationName: ORBITS[index], | ||
| animationTimingFunction: 'linear', | ||
| }, | ||
| { | ||
| backgroundColor: planet.color, | ||
| borderRadius: planet.size / 2, | ||
| height: planet.size, | ||
| width: planet.size, | ||
| elevation: { ':hover': 12, default: 0 }, | ||
| shadowColor: planet.color, | ||
| shadowOpacity: { ':hover': 0.9, default: 0 }, | ||
| shadowRadius: { ':hover': 18, default: 0 }, | ||
| transform: { | ||
| ':hover': [{ scale: 1.1 }], | ||
| default: [{ scale: 1 }], | ||
| }, | ||
| transitionDuration: '200ms', | ||
| }, | ||
| ]}> | ||
| <Animated.View | ||
| style={[ | ||
| styles.nameWrapper, | ||
| { | ||
| opacity: { ':active': 1, default: 0.02 }, | ||
| transitionDuration: '200ms', | ||
| }, | ||
| ]} | ||
| onStartShouldSetResponder={() => true}> | ||
| <Text style={styles.name}>{planet.name}</Text> | ||
| </Animated.View> | ||
| </Animated.View> | ||
| ))} | ||
| </Animated.View> | ||
| </Animated.View> | ||
| </VerticalExampleCard> | ||
| </Section> | ||
| </Scroll> | ||
| </Screen> | ||
| ); | ||
| } | ||
|
|
||
| const styles = StyleSheet.create({ | ||
| content: { | ||
| gap: spacing.xs, | ||
| }, | ||
| name: { | ||
| color: colors.white, | ||
| fontSize: 12, | ||
| fontWeight: '600', | ||
| textAlign: 'center', | ||
| }, | ||
| nameWrapper: { | ||
| alignItems: 'center', | ||
| bottom: 0, | ||
| justifyContent: 'center', | ||
| left: 0, | ||
| position: 'absolute', | ||
| right: 0, | ||
| top: 0, | ||
| }, | ||
| orbitPlane: { | ||
| height: PLANE_HEIGHT, | ||
| width: PLANE_WIDTH, | ||
| }, | ||
| orbiter: { | ||
| position: 'absolute', | ||
| shadowOffset: { height: 0, width: 0 }, | ||
| }, | ||
| stage: { | ||
| alignItems: 'center', | ||
| height: PLANE_HEIGHT + spacing.xl, | ||
| justifyContent: 'center', | ||
| }, | ||
| }); |
8 changes: 6 additions & 2 deletions
8
apps/common-app/src/apps/css/examples/transitions/screens/pseudoSelectors/index.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 |
|---|---|---|
| @@ -1,21 +1,25 @@ | ||
| import Active from './Active'; | ||
| import ActiveBlocksRender from './ActiveBlocksRender'; | ||
| import ActiveDeepest from './ActiveDeepest'; | ||
| import ArbitraryWebSelectors from './ArbitraryWebSelectors'; | ||
| import Focus from './Focus'; | ||
| import FocusWithin from './FocusWithin'; | ||
| import Form from './Form'; | ||
| import Hover from './Hover'; | ||
| import HoverWithLoop from './HoverWithLoop'; | ||
| import PerStateTransitionConfig from './PerStateTransitionConfig'; | ||
| import Showcase from './Showcase'; | ||
| import Planets from './Planets'; | ||
|
|
||
| export default { | ||
| Active, | ||
| ActiveBlocksRender, | ||
| ActiveDeepest, | ||
| ArbitraryWebSelectors, | ||
| Focus, | ||
| FocusWithin, | ||
| Form, | ||
| Hover, | ||
| HoverWithLoop, | ||
| PerStateTransitionConfig, | ||
| Showcase, | ||
| Planets, | ||
| }; |
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
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.
why this is needed?
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.
It makes the Animated.View a touch target so the press actually registers.; the alternative is wrapping in Pressable (like Form.tsx), but that's "heavier"
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.
But I will look into that - maybe we can easily change that so user doesn't have to do stuff like this.