Skip to content

Commit

Permalink
feat: allow to enable/disable touch ripple
Browse files Browse the repository at this point in the history
  • Loading branch information
nolimits4web committed Aug 27, 2021
1 parent b058c59 commit 0529a6b
Show file tree
Hide file tree
Showing 19 changed files with 78 additions and 14 deletions.
1 change: 0 additions & 1 deletion TODO.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,3 @@

- React
- DTS
- enable/disable touch ripple
3 changes: 2 additions & 1 deletion src/react/components/App.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const App = forwardRef((props, ref) => {

theme = 'material',
dark = true,
touchRipple = true,

// Children
children,
Expand All @@ -30,7 +31,7 @@ const App = forwardRef((props, ref) => {
};

return (
<TailwindMobileProvider theme={theme} dark={dark}>
<TailwindMobileProvider theme={theme} dark={dark} touchRipple={touchRipple}>
<Component
ref={elRef}
className={cls(`twm-${theme}`, className)}
Expand Down
4 changes: 3 additions & 1 deletion src/react/components/Button.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ const Button = forwardRef((props, ref) => {
segmentedStrong,
segmentedActive,

touchRipple = true,

// Children
children,

Expand All @@ -58,7 +60,7 @@ const Button = forwardRef((props, ref) => {
const themeClasses = useThemeClasses({ ios, material });
const dark = useDarkClasses();

useTouchRipple(rippleElRef, theme === 'material');
useTouchRipple(rippleElRef, theme === 'material' && touchRipple);

const size = large ? 'large' : small ? 'small' : 'medium';
let style = outline
Expand Down
4 changes: 3 additions & 1 deletion src/react/components/Fab.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ const Fab = forwardRef((props, ref) => {
ios,
material,

touchRipple = true,

// Children
children,

Expand All @@ -39,7 +41,7 @@ const Fab = forwardRef((props, ref) => {
const theme = useTheme({ ios, material });
const themeClasses = useThemeClasses({ ios, material });

useTouchRipple(rippleElRef, theme === 'material');
useTouchRipple(rippleElRef, theme === 'material' && touchRipple);

const colors = {
bg: 'bg-primary',
Expand Down
2 changes: 1 addition & 1 deletion src/react/components/Icon.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ const Icon = forwardRef((props, ref) => {
<Component ref={elRef} className={c.base} {...attrs}>
{theme === 'ios' ? ios : material}
{typeof badge !== 'undefined' && badge !== null && (
<Badge sm className={c.badge} colors={badgeColors}>
<Badge small className={c.badge} colors={badgeColors}>
{badge}
</Badge>
)}
Expand Down
3 changes: 2 additions & 1 deletion src/react/components/Link.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ const Link = forwardRef((props, ref) => {
tabbar,
tabbarActive,

touchRipple = true,
ios,
material,

Expand Down Expand Up @@ -50,7 +51,7 @@ const Link = forwardRef((props, ref) => {
const dark = useDarkClasses();

const needsTouchRipple =
theme === 'material' && (toolbar || tabbar || navbar);
theme === 'material' && (toolbar || tabbar || navbar) && touchRipple;

useTouchRipple(rippleElRef, needsTouchRipple);

Expand Down
4 changes: 3 additions & 1 deletion src/react/components/ListButton.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ const ListButton = forwardRef((props, ref) => {
ios,
material,

touchRipple = true,

// Children
children,

Expand All @@ -50,7 +52,7 @@ const ListButton = forwardRef((props, ref) => {
const theme = useTheme({ ios, material });
const themeClasses = useThemeClasses({ ios, material });

useTouchRipple(rippleElRef, theme === 'material');
useTouchRipple(rippleElRef, theme === 'material' && touchRipple);

const colors = {
text: 'text-primary',
Expand Down
5 changes: 4 additions & 1 deletion src/react/components/ListItem.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ const ListItem = forwardRef((props, ref) => {
ios,
material,

touchRipple = true,

// Children
children,

Expand Down Expand Up @@ -95,7 +97,8 @@ const ListItem = forwardRef((props, ref) => {
const isLink = !!href || href === '' || menuListItem || link;
const isLabel = !!label;

const needsTouchRipple = theme === 'material' && (isLabel || isLink);
const needsTouchRipple =
theme === 'material' && (isLabel || isLink) && touchRipple;

useTouchRipple(rippleElRef, needsTouchRipple);

Expand Down
6 changes: 4 additions & 2 deletions src/react/components/Stepper.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ const Stepper = forwardRef((props, ref) => {
ios,
material,

touchRipple = true,

children,
...rest
} = props;
Expand All @@ -52,8 +54,8 @@ const Stepper = forwardRef((props, ref) => {
const theme = useTheme({ ios, material });
const themeClasses = useThemeClasses({ ios, material });

useTouchRipple(buttonLeftElRef, theme === 'material');
useTouchRipple(buttonRightElRef, theme === 'material');
useTouchRipple(buttonLeftElRef, theme === 'material' && touchRipple);
useTouchRipple(buttonRightElRef, theme === 'material' && touchRipple);

const colors = {
text: 'text-primary',
Expand Down
6 changes: 5 additions & 1 deletion src/react/shared/TailwindMobileContext.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import React from 'react';

const TailwindMobileContext = React.createContext({ theme: 'ios', dark: true });
const TailwindMobileContext = React.createContext({
theme: 'ios',
dark: true,
touchRipple: true,
});

export { TailwindMobileContext };
4 changes: 2 additions & 2 deletions src/react/shared/TailwindMobileProvider.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ import React from 'react';
import { TailwindMobileContext } from './TailwindMobileContext.js';

const TailwindMobileProvider = (props) => {
const { theme, dark, children } = props;
const { theme, dark, touchRipple, children } = props;

return (
<TailwindMobileContext.Provider value={{ theme, dark }}>
<TailwindMobileContext.Provider value={{ theme, dark, touchRipple }}>
{children}
</TailwindMobileContext.Provider>
);
Expand Down
7 changes: 6 additions & 1 deletion src/react/shared/use-touch-ripple.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import { useRef, useEffect } from 'react';
import { useRef, useEffect, useContext } from 'react';
import { TailwindMobileContext } from './TailwindMobileContext.js';
import { TouchRipple } from './touch-ripple-class.js';

export const useTouchRipple = (elRef, needsTouchRipple) => {
const context = useContext(TailwindMobileContext);

const ripple = useRef(null);
const removeRipple = () => {
if (ripple.current) ripple.current.remove();
Expand All @@ -19,6 +22,8 @@ export const useTouchRipple = (elRef, needsTouchRipple) => {
};

const attachEvents = () => {
if (!context.touchRipple) return;

const el = elRef.current;
el.addEventListener('pointerdown', onPointerDown);
el.addEventListener('pointermove', onPointerMove);
Expand Down
6 changes: 6 additions & 0 deletions src/types/App.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,10 @@ interface Props {
* @default false
* */
dark?: boolean;
/**
* Enables touch ripple effect in Material theme. Allows to globally disable touch ripple for all components
*
* @default true
*/
touchRipple?: boolean;
}
7 changes: 7 additions & 0 deletions src/types/Button.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,4 +110,11 @@ interface Props {
* @default false
*/
segmentedActive?: boolean;

/**
* Enables touch ripple effect in Material theme
*
* @default true
*/
touchRipple?: boolean;
}
6 changes: 6 additions & 0 deletions src/types/Fab.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,10 @@ interface Props {
* Fab's icon
*/
icon?: React.ReactNode;
/**
* Enables touch ripple effect in Material theme
*
* @default true
*/
touchRipple?: boolean;
}
6 changes: 6 additions & 0 deletions src/types/Link.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,10 @@ interface Props {
* Click handler
*/
onClick?: (e: any) => void;
/**
* Enables touch ripple effect in Material theme
*
* @default true
*/
touchRipple?: boolean;
}
6 changes: 6 additions & 0 deletions src/types/ListButton.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,10 @@ interface Props {
* Object with additional props (attributes) to pass to the Link/Button
*/
linkProps?: any;
/**
* Enables touch ripple effect in Material theme
*
* @default true
*/
touchRipple?: boolean;
}
6 changes: 6 additions & 0 deletions src/types/ListItem.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,4 +159,10 @@ interface Props {
* List item link's `target` attribute
*/
target?: string;
/**
* Enables touch ripple effect in Material theme
*
* @default true
*/
touchRipple?: boolean;
}
6 changes: 6 additions & 0 deletions src/types/Stepper.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,12 @@ interface Props {
* @default false
*/
outline?: boolean;
/**
* Enables touch ripple effect in Material theme
*
* @default true
*/
touchRipple?: boolean;

/**
* Stepper input `change` handler
Expand Down

0 comments on commit 0529a6b

Please sign in to comment.