Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 | 93x 1616x 1616x 1616x 1616x 808x 808x 808x 808x 808x 6x 4x 5656x 1616x 1616x 808x 3232x 127x 808x 808x 808x 808x 808x 1616x 93x 13x 26x 26x 26x 26x 13x 13x | // @flow strict
import React, { useRef, useReducer, useEffect } from 'react';
import cx from 'classnames';
import * as util from '@material/ripple/util';
import MDCRippleFoundation from '@material/ripple/foundation';
import { applyPassive } from '@material/dom/events';
import { matches } from '@material/dom/ponyfill';
import type { AbstractComponent } from 'react';
import { mtStylesReducer, mtClassNamesReducer } from '../reducers';
import { getDisplayName } from '../utils';
import type { Style } from '../mt-types';
export const useRipple = <+A: HTMLElement, +S: HTMLElement>(
{
surface,
activator,
disabled = false,
unbounded = false,
primary = false,
accent = false,
}: {
activator: {| current: ?A |},
surface?: {| current: ?S |},
disabled?: boolean,
unbounded?: boolean,
primary?: boolean,
accent?: boolean,
},
partialAdapter: mixed,
) => {
const mdcRipple = useRef();
const [rippleStyle, distpachMTStyle] = useReducer(mtStylesReducer, {});
const [mtClassName, dispatchClassName] = useReducer(
mtClassNamesReducer,
new Set<string>(),
);
useEffect(() => {
let rippleActive = true;
const domActivator = activator.current;
const domSurface =
surface && surface.current ? surface.current : domActivator;
dispatchClassName({
type: 'reset',
className: cx({
'mdc-ripple-surface--primary': primary,
'mdc-ripple-surface--accent': accent,
}),
});
mdcRipple.current = new MDCRippleFoundation({
addClass: mClassName =>
rippleActive &&
dispatchClassName({ type: 'add', className: mClassName }),
removeClass: mClassName =>
rippleActive &&
dispatchClassName({ type: 'remove', className: mClassName }),
updateCssVariable: (varName, value) =>
rippleActive && distpachMTStyle({ type: 'set', name: varName, value }),
deregisterInteractionHandler: (evtType, handler) =>
domActivator &&
domActivator.removeEventListener(evtType, handler, applyPassive()),
registerInteractionHandler: (evtType, handler) =>
domActivator &&
domActivator.addEventListener(evtType, handler, applyPassive()),
browserSupportsCssVars: () => util.supportsCssVariables(window),
isUnbounded: () => unbounded,
isSurfaceActive: () => matches(domSurface, ':active'),
isSurfaceDisabled: () => disabled,
registerDocumentInteractionHandler: (evtType, handler) =>
document.documentElement &&
document.documentElement.addEventListener(
evtType,
handler,
applyPassive(),
),
deregisterDocumentInteractionHandler: (evtType, handler) =>
document.documentElement &&
document.documentElement.removeEventListener(
evtType,
handler,
applyPassive(),
),
registerResizeHandler: handler =>
window.addEventListener('resize', handler),
deregisterResizeHandler: handler =>
window.removeEventListener('resize', handler),
computeBoundingRect: () => {
if (
domSurface != null && // NOTE.- this compare null and undefined
domSurface.getBoundingClientRect
) {
return domSurface.getBoundingClientRect();
}
return {};
},
getWindowPageOffset: () => ({
x: window.pageXOffset,
y: window.pageYOffset,
}),
containsEventTarget: target => {
// NOTE.- this compare null and undefined
if (domActivator != null) {
domActivator.contains(target);
}
},
...partialAdapter,
});
mdcRipple.current.init();
return () => {
rippleActive = false;
Eif (mdcRipple.current) {
mdcRipple.current.destroy();
}
};
}, [
accent,
activator,
disabled,
partialAdapter,
primary,
surface,
unbounded,
]);
return {
ripple: mdcRipple.current,
rippleClassName: mtClassName,
rippleStyle,
};
};
const withRipple = <+A: HTMLElement, +S: HTMLElement, Config, Instance>(
Component: AbstractComponent<
{
...{|
rippleClassName: string,
rippleStyle: Style,
activator: {| current: ?A |},
surface?: {| current: ?S |},
|},
...Config,
},
Instance,
>,
{
unbounded,
primary,
accent,
disabled,
}: {|
unbounded?: boolean,
primary?: boolean,
accent?: boolean,
disabled?: boolean,
|} = {
unbounded: false,
primary: false,
accent: false,
disabled: false,
},
): AbstractComponent<Config, Instance> => {
const Rippled = React.forwardRef<Config, Instance>((props: Config, ref) => {
const surfaceRef = useRef<?HTMLElement>();
const activatorRef = useRef<?HTMLElement>();
const { rippleStyle, rippleClassName } = useRipple({
surface: surfaceRef,
activator: activatorRef,
unbounded,
primary,
accent,
disabled,
});
return (
<Component
ref={ref}
{...props}
rippleStyle={rippleStyle}
rippleClassName={cx('mdc-ripple-surface', Array.from(rippleClassName))}
activator={activatorRef}
surface={surfaceRef}
/>
);
});
Rippled.displayName = `WithRipple(${getDisplayName(Component)})`;
return Rippled;
};
export default withRipple;
|