1
- import { MDCRipple , MDCRippleFoundation } from '@material/ripple' ;
2
- import { getContext } from 'svelte' ;
1
+ import { MDCRipple , MDCRippleFoundation } from '@material/ripple' ;
2
+ import * as util from '@material/ripple/util' ;
3
+ import { applyPassive } from '@material/dom/events' ;
4
+ import { matches } from '@material/dom/ponyfill' ;
5
+ import { getContext } from 'svelte' ;
3
6
4
- export default function Ripple ( node , props = { ripple : false , unbounded : false , disabled : false , color : null , classForward : ( ) => { } } ) {
5
- let instance = null ;
7
+ export default function Ripple (
8
+ node ,
9
+ {
10
+ ripple = false ,
11
+ unbounded = false ,
12
+ disabled = false ,
13
+ color = null ,
14
+ addClass,
15
+ removeClass,
16
+ }
17
+ ) {
18
+ let instance ;
6
19
let addLayoutListener = getContext ( 'SMUI:addLayoutListener' ) ;
7
20
let removeLayoutListener ;
8
- let classList = [ ] ;
9
- let rippleCapableSurface = {
10
- get root_ ( ) {
11
- return node ;
12
- } ,
13
-
14
- get unbounded ( ) {
15
- return props . unbounded ;
16
- } ,
17
-
18
- set unbounded ( value ) {
19
- return props . unbounded = value ;
20
- } ,
21
-
22
- get disabled ( ) {
23
- return props . disabled ;
24
- } ,
25
-
26
- set disabled ( value ) {
27
- return props . disabled = value ;
28
- }
29
- } ;
30
21
31
- function addClass ( className ) {
32
- const idx = classList . indexOf ( className ) ;
33
- if ( idx === - 1 ) {
34
- node . classList . add ( className ) ;
35
- classList . push ( className ) ;
36
- if ( props . classForward ) {
37
- props . classForward ( classList ) ;
38
- }
39
- }
40
- }
41
-
42
- function removeClass ( className ) {
43
- const idx = classList . indexOf ( className ) ;
44
- if ( idx !== - 1 ) {
45
- node . classList . remove ( className ) ;
46
- classList . splice ( idx , 1 ) ;
47
- if ( props . classForward ) {
48
- props . classForward ( classList ) ;
49
- }
50
- }
22
+ function getAdapter ( ) {
23
+ return {
24
+ addClass,
25
+ browserSupportsCssVars : ( ) => util . supportsCssVariables ( window ) ,
26
+ computeBoundingRect : ( ) => node . getBoundingClientRect ( ) ,
27
+ containsEventTarget : ( target ) => node . contains ( target ) ,
28
+ deregisterDocumentInteractionHandler : ( evtType , handler ) =>
29
+ document . documentElement . removeEventListener (
30
+ evtType ,
31
+ handler ,
32
+ applyPassive ( )
33
+ ) ,
34
+ deregisterInteractionHandler : ( evtType , handler ) =>
35
+ node . removeEventListener ( evtType , handler , applyPassive ( ) ) ,
36
+ deregisterResizeHandler : ( handler ) =>
37
+ window . removeEventListener ( 'resize' , handler ) ,
38
+ getWindowPageOffset : ( ) => ( {
39
+ x : window . pageXOffset ,
40
+ y : window . pageYOffset ,
41
+ } ) ,
42
+ isSurfaceActive : ( ) => matches ( node , ':active' ) ,
43
+ isSurfaceDisabled : ( ) => ! ! disabled ,
44
+ isUnbounded : ( ) => ! ! unbounded ,
45
+ registerDocumentInteractionHandler : ( evtType , handler ) =>
46
+ document . documentElement . addEventListener (
47
+ evtType ,
48
+ handler ,
49
+ applyPassive ( )
50
+ ) ,
51
+ registerInteractionHandler : ( evtType , handler ) =>
52
+ node . addEventListener ( evtType , handler , applyPassive ( ) ) ,
53
+ registerResizeHandler : ( handler ) =>
54
+ window . addEventListener ( 'resize' , handler ) ,
55
+ removeClass,
56
+ updateCssVariable : ( varName , value ) =>
57
+ node . style . setProperty ( varName , value ) ,
58
+ } ;
51
59
}
52
60
53
61
function handleProps ( ) {
54
- if ( props . ripple && ! instance ) {
62
+ if ( ripple && ! instance ) {
55
63
// Override the Ripple component's adapter, so that we can forward classes
56
64
// to Svelte components that overwrite Ripple's classes.
57
- const foundation = new MDCRippleFoundation ( {
58
- ...MDCRipple . createAdapter ( rippleCapableSurface ) ,
59
- addClass : ( className ) => addClass ( className ) ,
60
- removeClass : ( className ) => removeClass ( className )
61
- } ) ;
62
- instance = new MDCRipple ( node , foundation ) ;
63
- } else if ( instance && ! props . ripple ) {
65
+ instance = new MDCRippleFoundation ( getAdapter ( ) ) ;
66
+ isntance . init ( ) ;
67
+ } else if ( instance && ! ripple ) {
64
68
instance . destroy ( ) ;
65
69
instance = null ;
66
70
}
67
- if ( props . ripple ) {
68
- instance . unbounded = ! ! props . unbounded ;
69
- switch ( props . color ) {
71
+ if ( ripple ) {
72
+ switch ( color ) {
70
73
case 'surface' :
71
74
addClass ( 'mdc-ripple-surface' ) ;
72
75
removeClass ( 'mdc-ripple-surface--primary' ) ;
@@ -102,8 +105,25 @@ export default function Ripple(node, props = {ripple: false, unbounded: false, d
102
105
}
103
106
104
107
return {
105
- update ( newProps = { ripple : false , unbounded : false , color : null , classForward : [ ] } ) {
106
- props = newProps ;
108
+ update ( props ) {
109
+ if ( 'ripple' in props ) {
110
+ ripple = props . ripple ;
111
+ }
112
+ if ( 'unbounded' in props ) {
113
+ unbounded = props . unbounded ;
114
+ }
115
+ if ( 'disabled' in props ) {
116
+ disabled = props . disabled ;
117
+ }
118
+ if ( 'color' in props ) {
119
+ color = props . color ;
120
+ }
121
+ if ( 'addClass' in props ) {
122
+ addClass = props . addClass ;
123
+ }
124
+ if ( 'removeClass' in props ) {
125
+ removeClass = props . removeClass ;
126
+ }
107
127
handleProps ( ) ;
108
128
} ,
109
129
@@ -119,6 +139,6 @@ export default function Ripple(node, props = {ripple: false, unbounded: false, d
119
139
if ( removeLayoutListener ) {
120
140
removeLayoutListener ( ) ;
121
141
}
122
- }
123
- }
142
+ } ,
143
+ } ;
124
144
}
0 commit comments