@@ -3,17 +3,16 @@ 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 type { ThemeProp } from '../../types' ;
1514
16- export type Props = $RemoveChildren < typeof TouchableRipple > & {
15+ export type Props = {
1716 /**
1817 * Status of checkbox.
1918 */
@@ -36,9 +35,9 @@ export type Props = $RemoveChildren<typeof TouchableRipple> & {
3635 color ?: ColorValue ;
3736 /**
3837 * 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.
38+ * (unchecked) and container (selected ) use `theme.colors.error`.
39+ * `disabled` and explicit `color`/`uncheckedColor` overrides take
40+ * precedence.
4241 */
4342 error ?: boolean ;
4443 /**
@@ -51,7 +50,14 @@ export type Props = $RemoveChildren<typeof TouchableRipple> & {
5150 testID ?: string ;
5251} ;
5352
54- const ANIMATION_DURATION = 100 ;
53+ // Spec dimensions (https://m3.material.io/components/checkbox/specs).
54+ const CONTAINER_SIZE = 18 ;
55+ const CONTAINER_RADIUS = 2 ;
56+ const OUTLINE_WIDTH = 2 ;
57+ const STATE_LAYER_SIZE = 40 ;
58+ const FILL_DURATION = 100 ;
59+ const CHECK_DURATION = 150 ;
60+
5561
5662/**
5763 * Checkboxes allow the selection of multiple options from a set.
@@ -84,121 +90,234 @@ const Checkbox = ({
8490 onPress,
8591 testID,
8692 error,
87- ...rest
93+ color,
94+ uncheckedColor,
8895} : Props ) => {
8996 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 ) ;
97+ const focusVisible = false ;
98+ const [ hovered , setHovered ] = React . useState ( false ) ;
99+ const [ pressed , setPressed ] = React . useState ( false ) ;
100+
101+ const selected = status === 'checked' || status === 'indeterminate' ;
94102
95103 const {
96104 animation : { scale } ,
97105 } = theme ;
98106
107+ // 0 = unselected (outline only), 1 = selected (filled + drawn icon).
108+ const fillAnim = React . useRef ( new Animated . Value ( selected ? 1 : 0 ) ) . current ;
109+ const checkAnim = React . useRef ( new Animated . Value ( selected ? 1 : 0 ) ) . current ;
110+ const firstRender = React . useRef ( true ) ;
111+
99112 React . useEffect ( ( ) => {
100- // Do not run animation on very first rendering
101- if ( isFirstRendering . current ) {
102- isFirstRendering . current = false ;
113+ if ( firstRender . current ) {
114+ firstRender . current = false ;
103115 return ;
104116 }
117+ Animated . timing ( fillAnim , {
118+ toValue : selected ? 1 : 0 ,
119+ duration : FILL_DURATION * scale ,
120+ useNativeDriver : true ,
121+ } ) . start ( ) ;
122+ Animated . timing ( checkAnim , {
123+ toValue : selected ? 1 : 0 ,
124+ duration : CHECK_DURATION * scale ,
125+ useNativeDriver : false ,
126+ } ) . start ( ) ;
127+ } , [ selected , fillAnim , checkAnim , scale ] ) ;
105128
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 ] ,
129+ const visual = getSelectionVisualState ( {
130+ theme,
131+ selected,
132+ disabled,
133+ hovered,
134+
135+ pressed,
136+ error,
137+ customColor : color ,
138+ customUncheckedColor : uncheckedColor ,
140139 } ) ;
141140
142- const icon = indeterminate
143- ? 'minus-box'
144- : checked
145- ? 'checkbox-marked'
146- : 'checkbox-blank-outline' ;
141+ // Outline fades out as fill fades in (and vice versa).
142+ const outlineOpacity = fillAnim . interpolate ( {
143+ inputRange : [ 0 , 1 ] ,
144+ outputRange : [ 1 , 0 ] ,
145+ } ) ;
146+
147+ // Remember which glyph to render so the reveal-mask can still collapse
148+ // when transitioning back to 'unchecked' (selected becomes false, but
149+ // we keep showing the previous glyph until checkAnim hits 0).
150+ const lastGlyph = React . useRef < 'check' | 'indeterminate' > ( 'check' ) ;
151+ if ( status === 'checked' ) lastGlyph . current = 'check' ;
152+ else if ( status === 'indeterminate' ) lastGlyph . current = 'indeterminate' ;
153+ const showIndeterminate = lastGlyph . current === 'indeterminate' ;
147154
148155 return (
149- < TouchableRipple
150- { ...rest }
151- borderless
156+ < Pressable
152157 onPress = { onPress }
158+ onHoverIn = { ( ) => setHovered ( true ) }
159+ onHoverOut = { ( ) => setHovered ( false ) }
160+ onPressIn = { ( ) => setPressed ( true ) }
161+ onPressOut = { ( ) => setPressed ( false ) }
153162 disabled = { disabled }
154163 accessibilityRole = "checkbox"
155- accessibilityState = { { disabled, checked } }
164+ accessibilityState = { { disabled, checked : status === 'checked' } }
156165 accessibilityLiveRegion = "polite"
157- style = { styles . container }
158166 testID = { testID }
159- theme = { theme }
167+ style = { styles . tapTarget }
160168 >
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"
169+ < View pointerEvents = "none" style = { styles . tapTargetInner } >
170+ < View
171+ style = { [
172+ styles . stateLayer ,
173+ {
174+ backgroundColor : visual . stateLayerColor ,
175+ opacity : visual . stateLayerOpacity ,
176+ } ,
177+ ] }
173178 />
174- < View style = { [ StyleSheet . absoluteFill , styles . fillContainer ] } >
179+
180+ < View style = { [ styles . container , { opacity : visual . containerOpacity } ] } >
181+ < Animated . View
182+ pointerEvents = "none"
183+ style = { [
184+ styles . outline ,
185+ {
186+ borderColor : visual . outlineColor ,
187+ opacity : outlineOpacity ,
188+ } ,
189+ ] }
190+ />
175191 < Animated . View
192+ pointerEvents = "none"
176193 style = { [
177194 styles . fill ,
178- { borderColor : selectionControlColor } ,
179- { borderWidth } ,
195+ {
196+ backgroundColor : visual . containerColor ,
197+ opacity : fillAnim ,
198+ } ,
180199 ] }
181200 />
201+ { showIndeterminate ? (
202+ < Animated . View
203+ style = { [
204+ styles . checkmarkMask ,
205+ {
206+ width : checkAnim . interpolate ( { inputRange : [ 0 , 1 ] , outputRange : [ 0 , CONTAINER_SIZE ] } ) ,
207+ opacity : checkAnim ,
208+ } ,
209+ ] }
210+ >
211+ < View style = { styles . checkmarkContent } >
212+ < View style = { [ styles . dash , { backgroundColor : visual . iconColor } ] } />
213+ </ View >
214+ </ Animated . View >
215+ ) : (
216+ < Checkmark color = { visual . iconColor } progress = { checkAnim } />
217+ ) }
182218 </ View >
183- </ Animated . View >
184- </ TouchableRipple >
219+ </ View >
220+ </ Pressable >
221+ ) ;
222+ } ;
223+
224+ /**
225+ * Reveal-mask checkmark: a static L-shape (borderLeftWidth +
226+ * borderBottomWidth rotated -45deg) inside a left-anchored View whose
227+ * width animates 0 -> CONTAINER_SIZE. The checkmark "draws in"
228+ * left-to-right, approximating Compose Material3's stroke-fraction
229+ * animation without an SVG dependency.
230+ */
231+ const Checkmark = ( {
232+ color,
233+ progress,
234+ } : {
235+ color : ColorValue ;
236+ progress : Animated . Value ;
237+ } ) => {
238+ const maskWidth = progress . interpolate ( {
239+ inputRange : [ 0 , 1 ] ,
240+ outputRange : [ 0 , CONTAINER_SIZE ] ,
241+ } ) ;
242+ return (
243+ < Animated . View style = { [ styles . checkmarkMask , { width : maskWidth , opacity : progress } ] } >
244+ < View style = { styles . checkmarkContent } >
245+ < View style = { [ styles . checkmarkGlyph , { borderColor : color } ] } />
246+ </ View >
247+ </ Animated . View >
185248 ) ;
186249} ;
187250
188251const styles = StyleSheet . create ( {
189- container : {
190- borderRadius : 18 ,
191- width : 36 ,
192- height : 36 ,
193- padding : 6 ,
252+ tapTarget : {
253+ width : STATE_LAYER_SIZE ,
254+ height : STATE_LAYER_SIZE ,
255+ alignItems : 'center' ,
256+ justifyContent : 'center' ,
194257 } ,
195- fillContainer : {
258+ tapTargetInner : {
259+ width : STATE_LAYER_SIZE ,
260+ height : STATE_LAYER_SIZE ,
261+ alignItems : 'center' ,
262+ justifyContent : 'center' ,
263+ } ,
264+ stateLayer : {
265+ position : 'absolute' ,
266+ top : 0 ,
267+ left : 0 ,
268+ width : STATE_LAYER_SIZE ,
269+ height : STATE_LAYER_SIZE ,
270+ borderRadius : STATE_LAYER_SIZE / 2 ,
271+ } ,
272+ container : {
273+ width : CONTAINER_SIZE ,
274+ height : CONTAINER_SIZE ,
275+ borderRadius : CONTAINER_RADIUS ,
196276 alignItems : 'center' ,
197277 justifyContent : 'center' ,
278+ overflow : 'hidden' ,
198279 } ,
199280 fill : {
200- height : 14 ,
201- width : 14 ,
281+ position : 'absolute' ,
282+ top : 0 ,
283+ left : 0 ,
284+ right : 0 ,
285+ bottom : 0 ,
286+ borderRadius : CONTAINER_RADIUS ,
287+ } ,
288+ outline : {
289+ position : 'absolute' ,
290+ top : 0 ,
291+ left : 0 ,
292+ right : 0 ,
293+ bottom : 0 ,
294+ borderWidth : OUTLINE_WIDTH ,
295+ borderRadius : CONTAINER_RADIUS ,
296+ } ,
297+ dash : {
298+ width : 10 ,
299+ height : 2 ,
300+ borderRadius : 1 ,
301+ } ,
302+ checkmarkMask : {
303+ position : 'absolute' ,
304+ left : 0 ,
305+ top : 0 ,
306+ height : CONTAINER_SIZE ,
307+ overflow : 'hidden' ,
308+ } ,
309+ checkmarkContent : {
310+ width : CONTAINER_SIZE ,
311+ height : CONTAINER_SIZE ,
312+ alignItems : 'center' ,
313+ justifyContent : 'center' ,
314+ } ,
315+ checkmarkGlyph : {
316+ width : 11 ,
317+ height : 6 ,
318+ borderLeftWidth : 2 ,
319+ borderBottomWidth : 2 ,
320+ transform : [ { rotate : '-45deg' } , { translateY : - 1 } , { translateX : 1 } ] ,
202321 } ,
203322} ) ;
204323
0 commit comments