@@ -3,17 +3,18 @@ import {
33 Animated ,
44 ColorValue ,
55 GestureResponderEvent ,
6+ Pressable ,
67 StyleSheet ,
78 View ,
89} from 'react-native' ;
910
10- import { getSelectionControlColor } from './utils' ;
11+ import { getSelectionVisualState } from './utils' ;
1112import { useInternalTheme } from '../../core/theming' ;
12- import type { $RemoveChildren , ThemeProp } from '../../types ' ;
13- import MaterialCommunityIcon from '../MaterialCommunityIcon ' ;
14- import TouchableRipple from '../TouchableRipple/TouchableRipple ' ;
13+ import { tokens } from '../../theme/tokens ' ;
14+ import type { ThemeProp } from '../../types ' ;
15+ import { useFocusVisible } from '../../utils/useFocusVisible ' ;
1516
16- export type Props = $RemoveChildren < typeof TouchableRipple > & {
17+ export type Props = {
1718 /**
1819 * Status of checkbox.
1920 */
@@ -36,9 +37,9 @@ export type Props = $RemoveChildren<typeof TouchableRipple> & {
3637 color ?: ColorValue ;
3738 /**
3839 * Whether the checkbox is in an error state. When true, the outline
39- * (unchecked) and container (checked / indeterminate ) use
40- * `theme.colors.error`. ` disabled` and explicit `color`/`uncheckedColor`
41- * overrides take precedence.
40+ * (unchecked) and container (selected ) use `theme.colors.error`.
41+ * `disabled` and explicit `color`/`uncheckedColor` overrides take
42+ * precedence.
4243 */
4344 error ?: boolean ;
4445 /**
@@ -51,7 +52,15 @@ export type Props = $RemoveChildren<typeof TouchableRipple> & {
5152 testID ?: string ;
5253} ;
5354
54- const ANIMATION_DURATION = 100 ;
55+ // Spec dimensions (https://m3.material.io/components/checkbox/specs).
56+ const CONTAINER_SIZE = 18 ;
57+ const CONTAINER_RADIUS = 2 ;
58+ const OUTLINE_WIDTH = 2 ;
59+ const STATE_LAYER_SIZE = 40 ;
60+ const FILL_DURATION = 100 ;
61+ const CHECK_DURATION = 150 ;
62+
63+ const { focusIndicator } = tokens . md . sys . state ;
5564
5665/**
5766 * Checkboxes allow the selection of multiple options from a set.
@@ -84,121 +93,254 @@ const Checkbox = ({
8493 onPress,
8594 testID,
8695 error,
87- ...rest
96+ color,
97+ uncheckedColor,
8898} : Props ) => {
8999 const theme = useInternalTheme ( themeOverrides ) ;
90- const { current : scaleAnim } = React . useRef < Animated . Value > (
91- new Animated . Value ( 1 )
92- ) ;
93- const isFirstRendering = React . useRef < boolean > ( true ) ;
100+ const { focusVisible, onFocus, onBlur } = useFocusVisible ( ) ;
101+ const [ hovered , setHovered ] = React . useState ( false ) ;
102+ const [ pressed , setPressed ] = React . useState ( false ) ;
103+
104+ const selected = status === 'checked' || status === 'indeterminate' ;
94105
95106 const {
96107 animation : { scale } ,
97108 } = theme ;
98109
110+ // 0 = unselected (outline only), 1 = selected (filled + drawn icon).
111+ const fillAnim = React . useRef ( new Animated . Value ( selected ? 1 : 0 ) ) . current ;
112+ const checkAnim = React . useRef ( new Animated . Value ( selected ? 1 : 0 ) ) . current ;
113+ const firstRender = React . useRef ( true ) ;
114+
99115 React . useEffect ( ( ) => {
100- // Do not run animation on very first rendering
101- if ( isFirstRendering . current ) {
102- isFirstRendering . current = false ;
116+ if ( firstRender . current ) {
117+ firstRender . current = false ;
103118 return ;
104119 }
120+ Animated . timing ( fillAnim , {
121+ toValue : selected ? 1 : 0 ,
122+ duration : FILL_DURATION * scale ,
123+ useNativeDriver : true ,
124+ } ) . start ( ) ;
125+ Animated . timing ( checkAnim , {
126+ toValue : selected ? 1 : 0 ,
127+ duration : CHECK_DURATION * scale ,
128+ useNativeDriver : false ,
129+ } ) . start ( ) ;
130+ } , [ selected , fillAnim , checkAnim , scale ] ) ;
105131
106- const checked = status === 'checked' ;
107-
108- Animated . sequence ( [
109- Animated . timing ( scaleAnim , {
110- toValue : 0.85 ,
111- duration : checked ? ANIMATION_DURATION * scale : 0 ,
112- useNativeDriver : false ,
113- } ) ,
114- Animated . timing ( scaleAnim , {
115- toValue : 1 ,
116- duration : checked
117- ? ANIMATION_DURATION * scale
118- : ANIMATION_DURATION * scale * 1.75 ,
119- useNativeDriver : false ,
120- } ) ,
121- ] ) . start ( ) ;
122- } , [ status , scaleAnim , scale ] ) ;
123-
124- const checked = status === 'checked' ;
125- const indeterminate = status === 'indeterminate' ;
126-
127- const { selectionControlColor, selectionControlOpacity } =
128- getSelectionControlColor ( {
129- theme,
130- disabled,
131- checked,
132- customColor : rest . color ,
133- customUncheckedColor : rest . uncheckedColor ,
134- error,
135- } ) ;
136-
137- const borderWidth = scaleAnim . interpolate ( {
138- inputRange : [ 0.8 , 1 ] ,
139- outputRange : [ 7 , 0 ] ,
132+ const visual = getSelectionVisualState ( {
133+ theme,
134+ selected,
135+ disabled,
136+ hovered,
137+ focused : focusVisible ,
138+ pressed,
139+ error,
140+ customColor : color ,
141+ customUncheckedColor : uncheckedColor ,
140142 } ) ;
141143
142- const icon = indeterminate
143- ? 'minus-box'
144- : checked
145- ? 'checkbox-marked'
146- : 'checkbox-blank-outline' ;
144+ // Outline fades out as fill fades in (and vice versa).
145+ const outlineOpacity = fillAnim . interpolate ( {
146+ inputRange : [ 0 , 1 ] ,
147+ outputRange : [ 1 , 0 ] ,
148+ } ) ;
149+
150+ // Remember which glyph to render so the reveal-mask can still collapse
151+ // when transitioning back to 'unchecked' (selected becomes false, but
152+ // we keep showing the previous glyph until checkAnim hits 0).
153+ const lastGlyph = React . useRef < 'check' | 'indeterminate' > ( 'check' ) ;
154+ if ( status === 'checked' ) lastGlyph . current = 'check' ;
155+ else if ( status === 'indeterminate' ) lastGlyph . current = 'indeterminate' ;
156+ const showIndeterminate = lastGlyph . current === 'indeterminate' ;
147157
148158 return (
149- < TouchableRipple
150- { ...rest }
151- borderless
159+ < Pressable
152160 onPress = { onPress }
161+ onFocus = { onFocus }
162+ onBlur = { onBlur }
163+ onHoverIn = { ( ) => setHovered ( true ) }
164+ onHoverOut = { ( ) => setHovered ( false ) }
165+ onPressIn = { ( ) => setPressed ( true ) }
166+ onPressOut = { ( ) => setPressed ( false ) }
153167 disabled = { disabled }
154168 accessibilityRole = "checkbox"
155- accessibilityState = { { disabled, checked } }
169+ accessibilityState = { { disabled, checked : status === 'checked' } }
156170 accessibilityLiveRegion = "polite"
157- style = { styles . container }
158171 testID = { testID }
159- theme = { theme }
172+ style = { styles . tapTarget }
160173 >
161- < Animated . View
162- style = { {
163- transform : [ { scale : scaleAnim } ] ,
164- opacity : selectionControlOpacity ,
165- } }
166- >
167- < MaterialCommunityIcon
168- allowFontScaling = { false }
169- name = { icon }
170- size = { 24 }
171- color = { selectionControlColor }
172- direction = "ltr"
174+ < View pointerEvents = "none" style = { styles . tapTargetInner } >
175+ < View
176+ style = { [
177+ styles . stateLayer ,
178+ {
179+ backgroundColor : visual . stateLayerColor ,
180+ opacity : visual . stateLayerOpacity ,
181+ } ,
182+ ] }
173183 />
174- < View style = { [ StyleSheet . absoluteFill , styles . fillContainer ] } >
184+ { focusVisible && ! disabled ? (
185+ < View
186+ style = { [
187+ styles . focusRing ,
188+ {
189+ borderColor : theme . colors . secondary ,
190+ borderWidth : focusIndicator . thickness ,
191+ } ,
192+ ] }
193+ />
194+ ) : null }
195+ < View style = { [ styles . container , { opacity : visual . containerOpacity } ] } >
175196 < Animated . View
197+ pointerEvents = "none"
198+ style = { [
199+ styles . outline ,
200+ {
201+ borderColor : visual . outlineColor ,
202+ opacity : outlineOpacity ,
203+ } ,
204+ ] }
205+ />
206+ < Animated . View
207+ pointerEvents = "none"
176208 style = { [
177209 styles . fill ,
178- { borderColor : selectionControlColor } ,
179- { borderWidth } ,
210+ {
211+ backgroundColor : visual . containerColor ,
212+ opacity : fillAnim ,
213+ } ,
180214 ] }
181215 />
216+ { showIndeterminate ? (
217+ < Animated . View
218+ style = { [
219+ styles . checkmarkMask ,
220+ {
221+ width : checkAnim . interpolate ( { inputRange : [ 0 , 1 ] , outputRange : [ 0 , CONTAINER_SIZE ] } ) ,
222+ opacity : checkAnim ,
223+ } ,
224+ ] }
225+ >
226+ < View style = { styles . checkmarkContent } >
227+ < View style = { [ styles . dash , { backgroundColor : visual . iconColor } ] } />
228+ </ View >
229+ </ Animated . View >
230+ ) : (
231+ < Checkmark color = { visual . iconColor } progress = { checkAnim } />
232+ ) }
182233 </ View >
183- </ Animated . View >
184- </ TouchableRipple >
234+ </ View >
235+ </ Pressable >
236+ ) ;
237+ } ;
238+
239+ /**
240+ * Reveal-mask checkmark: a static L-shape (borderLeftWidth +
241+ * borderBottomWidth rotated -45deg) inside a left-anchored View whose
242+ * width animates 0 -> CONTAINER_SIZE. The checkmark "draws in"
243+ * left-to-right, approximating Compose Material3's stroke-fraction
244+ * animation without an SVG dependency.
245+ */
246+ const Checkmark = ( {
247+ color,
248+ progress,
249+ } : {
250+ color : ColorValue ;
251+ progress : Animated . Value ;
252+ } ) => {
253+ const maskWidth = progress . interpolate ( {
254+ inputRange : [ 0 , 1 ] ,
255+ outputRange : [ 0 , CONTAINER_SIZE ] ,
256+ } ) ;
257+ return (
258+ < Animated . View style = { [ styles . checkmarkMask , { width : maskWidth , opacity : progress } ] } >
259+ < View style = { styles . checkmarkContent } >
260+ < View style = { [ styles . checkmarkGlyph , { borderColor : color } ] } />
261+ </ View >
262+ </ Animated . View >
185263 ) ;
186264} ;
187265
188266const styles = StyleSheet . create ( {
189- container : {
190- borderRadius : 18 ,
191- width : 36 ,
192- height : 36 ,
193- padding : 6 ,
267+ tapTarget : {
268+ width : STATE_LAYER_SIZE ,
269+ height : STATE_LAYER_SIZE ,
270+ alignItems : 'center' ,
271+ justifyContent : 'center' ,
272+ } ,
273+ tapTargetInner : {
274+ width : STATE_LAYER_SIZE ,
275+ height : STATE_LAYER_SIZE ,
276+ alignItems : 'center' ,
277+ justifyContent : 'center' ,
278+ } ,
279+ stateLayer : {
280+ position : 'absolute' ,
281+ top : 0 ,
282+ left : 0 ,
283+ width : STATE_LAYER_SIZE ,
284+ height : STATE_LAYER_SIZE ,
285+ borderRadius : STATE_LAYER_SIZE / 2 ,
194286 } ,
195- fillContainer : {
287+ focusRing : {
288+ position : 'absolute' ,
289+ top : - focusIndicator . outerOffset ,
290+ left : - focusIndicator . outerOffset ,
291+ width : STATE_LAYER_SIZE + focusIndicator . outerOffset * 2 ,
292+ height : STATE_LAYER_SIZE + focusIndicator . outerOffset * 2 ,
293+ borderRadius : ( STATE_LAYER_SIZE + focusIndicator . outerOffset * 2 ) / 2 ,
294+ } ,
295+ container : {
296+ width : CONTAINER_SIZE ,
297+ height : CONTAINER_SIZE ,
298+ borderRadius : CONTAINER_RADIUS ,
196299 alignItems : 'center' ,
197300 justifyContent : 'center' ,
301+ overflow : 'hidden' ,
198302 } ,
199303 fill : {
200- height : 14 ,
201- width : 14 ,
304+ position : 'absolute' ,
305+ top : 0 ,
306+ left : 0 ,
307+ right : 0 ,
308+ bottom : 0 ,
309+ borderRadius : CONTAINER_RADIUS ,
310+ } ,
311+ outline : {
312+ position : 'absolute' ,
313+ top : 0 ,
314+ left : 0 ,
315+ right : 0 ,
316+ bottom : 0 ,
317+ borderWidth : OUTLINE_WIDTH ,
318+ borderRadius : CONTAINER_RADIUS ,
319+ } ,
320+ dash : {
321+ width : 10 ,
322+ height : 2 ,
323+ borderRadius : 1 ,
324+ } ,
325+ checkmarkMask : {
326+ position : 'absolute' ,
327+ left : 0 ,
328+ top : 0 ,
329+ height : CONTAINER_SIZE ,
330+ overflow : 'hidden' ,
331+ } ,
332+ checkmarkContent : {
333+ width : CONTAINER_SIZE ,
334+ height : CONTAINER_SIZE ,
335+ alignItems : 'center' ,
336+ justifyContent : 'center' ,
337+ } ,
338+ checkmarkGlyph : {
339+ width : 11 ,
340+ height : 6 ,
341+ borderLeftWidth : 2 ,
342+ borderBottomWidth : 2 ,
343+ transform : [ { rotate : '-45deg' } , { translateY : - 1 } , { translateX : 1 } ] ,
202344 } ,
203345} ) ;
204346
0 commit comments