1- import type {
1+ import {
22 FC ,
33 HTMLAttributes ,
44 ReactNode ,
@@ -12,7 +12,11 @@ import { M3Ripple } from '@/components/ripple'
1212
1313import { useRef } from 'react'
1414
15- import makeId from '@/utils/id'
15+ import {
16+ useElementEffect ,
17+ useId ,
18+ useTarget ,
19+ } from '@/hooks'
1620
1721import { compose } from '@/utils/events'
1822import {
@@ -62,7 +66,7 @@ const Subheading: FC<HTMLAttributes<HTMLElement>> = ({
6266)
6367
6468const M3Card : FC < M3CardProps > = ( {
65- id = makeId ( 'm3-card' ) ,
69+ id,
6670 appearance = 'filled' ,
6771 heading = '' ,
6872 subheading = '' ,
@@ -74,8 +78,10 @@ const M3Card: FC<M3CardProps> = ({
7478 onClick = ( _ ) => { } ,
7579 ...attrs
7680} ) => {
81+ const _id = useId ( id , 'm3-card' )
7782 const state = useRef < HTMLDivElement | null > ( null )
7883 const ripple = useRef < M3RippleMethods | null > ( null )
84+ const [ rippleTarget , setRippleTarget ] = useTarget < HTMLDivElement > ( )
7985
8086 const [ slots , content , hasSlot ] = distinct ( children , {
8187 content : Content ,
@@ -84,18 +90,20 @@ const M3Card: FC<M3CardProps> = ({
8490 subheading : Subheading ,
8591 } )
8692
93+ useElementEffect ( state , setRippleTarget )
94+
8795 const hasHeading = hasSlot ( 'heading' ) || heading . length > 0
8896 const hasSubheading = hasSlot ( 'subheading' ) || subheading . length > 0
8997
9098 const headingId = slots . heading ?. props . id ?? null
9199 const headingEl = slots . heading
92- ? headingId ? slots . heading : augment ( slots . heading , { id : id + '-heading' } )
100+ ? headingId ? slots . heading : augment ( slots . heading , { id : _id + '-heading' } )
93101 : heading . length
94- ? < Heading id = { id + '-heading' } children = { heading } />
102+ ? < Heading id = { _id + '-heading' } children = { heading } />
95103 : null
96104
97105 const aria = ! ( 'aria-label' in attrs ) && ! hasSlot ( 'content' ) && hasHeading ? {
98- 'aria-labelledby' : headingId ?? id + '-heading' ,
106+ 'aria-labelledby' : headingId ?? _id + '-heading' ,
99107 } : { }
100108
101109 return (
@@ -109,18 +117,18 @@ const M3Card: FC<M3CardProps> = ({
109117 role = { role }
110118 onClick = { compose ( event => {
111119 if ( interactive ) {
112- ripple . current ?. activate ( event )
120+ ripple . current ?. activate ( event . nativeEvent )
113121 }
114122 } , onClick ) }
115123 { ...{
116- ...( interactive ? { tabindex : 0 } : { } ) ,
124+ ...( interactive ? { tabIndex : 0 } : { } ) ,
117125 ...aria ,
118126 ...attrs ,
119127 } }
120128 >
121129 { interactive ? (
122130 < div ref = { state } className = "m3-card__state" >
123- < M3Ripple ref = { ripple } owner = { state } />
131+ < M3Ripple ref = { ripple } owner = { rippleTarget } />
124132 </ div >
125133 ) : null }
126134 { slots . content ?? ( < >
0 commit comments