Better hover, active and focus states than CSS pseudo-classes, and a callback when the interactive state changes.
npm install react-interactive  !npm type definitions
- Better interactive states than CSS pseudo classes
- hover, active, mouseActive, touchActive, keyActive
- focus, focusFromMouse, focusFromTouch, focusFromKey
- Callback for interactive state changes
- Know when the hover/active/focus state is entered/exited (impossible to do with CSS)
- Style interactive states with CSS, inline styles, and CSS-in-JS libraries
- Eliminates the CSS sticky :hover bug on touch devices
- Allows you to only add focus styles when focus is from the keyboard
---
Code is in the /demo folder, or open the demo in CodeSandbox
---
Basics ⚡️ Props ⚡️ createInteractive ⚡️ eventFrom ⚡️ TypeScript ⚡️ FAQ
---
Install, as prop, Interactive state, CSS, CSS-in-JS, Inline styles, Interactive state changes, Interactive state in children, Extending
---
``shell`
npm install --save react-interactive
`js
import { Interactive } from 'react-interactive';
const App = () =>
`
---
React Interactive accepts a polymorphic as prop that can be a string representing a DOM element (e.g. "button", "a", "div", etc), or a React component (e.g. React Router's Link, etc).
`js
import { Interactive } from 'react-interactive';
import { Link } from 'react-router-dom';
const App = () => (
<>
My Link
My React Router Link
>
);
`
---
The state object used by React Interactive to determine how the component is rendered. The interactive state object is also passed to the onStateChange callback and children (when children is a function).
`ts`
interface InteractiveState {
hover: boolean;
active: 'mouseActive' | 'touchActive' | 'keyActive' | false;
focus: 'focusFromMouse' | 'focusFromTouch' | 'focusFromKey' | false;
}
- hover Mouse on the element (unlike CSS pseudo classes the hover state is only entered from mouse input which eliminates the CSS sticky :hover bug on touch devices).active
- mouseActive
- Mouse on the element and mouse button down.touchActive
- Touch point on the element.keyActive
- Element has focus and the enter key is down (or space bar for some elements).focus
- focusFromMouse
- Element has focus and focus was entered from mouse input.focusFromTouch
- Element has focus and focus was entered from touch input.focusFromKey
- Element has focus and focus was entered from keyboard input (e.g. tab key).
---
CSS classes for the current state are automatically added for easy styling with CSS or CSS-in-JS libraries like Styled Components, Emotion, and Stitches.
- Hover state adds a hover class.active
- Active state adds both an class and an [input]Active class, e.g. mouseActive.focus
- Focus state adds both a class and a focusFrom[input] class, e.g. focusFromKey.className
- For a full class list see interactive props (the class names for each state can be changed using props).
- See this CodeSandbox for a live example using CSS.
`js
import { Interactive } from 'react-interactive';
const App = () => (
// add a className to target the element in CSS
My Button
);
`
`css
/ use compound selectors in CSS to style the interactive states /
.my-button.hover, .my-button.active: {
color: green;
}
.my-button.focusfromkey: {
outline: 2px solid green;
}
`
---
Use the added CSS classes to style the interactive states with CSS-in-JS libraries like Styled Components, Emotion, and Stitches. Live examples in CodeSandbox are available for Styled Components and Stitches (also the demo app is built using Stitches).
> React Interactive includes a createInteractive(as) function with some predefined DOM elements, for example Interactive.Button, for easy use with CSS-in-JS. For more see Extending .
`js
import { Interactive } from 'react-interactive';
import { styled } from '@stitches/react';
const StyledButton = styled(Interactive.Button, {
'&.hover, &.active': {
color: 'green',
},
'&.focusFromKey': {
outline: '2px solid green',
},
});
const App = () =>
`
---
React Interactive uses a separate style prop for each state for easy inline styling.
- Hover state uses the hoverStyle prop.activeStyle
- Active state uses both an prop and an [input]ActiveStyle prop.focusStyle
- Focus state uses both a prop and a focusFrom[input]Style prop.style
- For a full list see interactive props.
- See this CodeSandbox for a live example using inline styles.
`js
import { Interactive } from 'react-interactive';
const hoverAndActiveStyle = {
color: 'green',
};
const focusFromKeyStyle = {
outline: '2px solid green',
};
const App = () => (
hoverStyle={hoverAndActiveStyle}
activeStyle={hoverAndActiveStyle}
focusFromKeyStyle={focusFromKeyStyle}
>
My Button
);
`
---
React Interactive accepts an onStateChange prop callback that is called each time the state changes with both the current and previous states.
`js
import * as React from 'react';
import { Interactive } from 'react-interactive';
const App = () => {
const handleInteractiveStateChange = React.useCallback(
({ state, prevState }) => {
// both state and prevState are of the shape:
// {
// hover: boolean,
// active: 'mouseActive' | 'touchActive' | 'keyActive' | false,
// focus: 'focusFromMouse' | 'focusFromTouch' | 'focusFromKey' | false,
// }
},
[],
);
return (
My Button
);
};
`
---
React Interactive uses the children as a function pattern to pass the current interactive state to its children.
`js
import { Interactive } from 'react-interactive';
const App = () => (
{({ hover, active, focus }) =>
Current state - active: ${active}, hover: ${hover}, focus: ${focus}
}
);
`
---
Sometimes it is useful to extend the polymorphic component with a predefined as prop, but without additional logic. This is especially useful when using React Interactive with CSS-in-JS libraries and other polymorphic components.
React Interactive provides a createInteractive(as) function that returns a fully typed component with the as prop predefined. Also, some common DOM elements are available using Interactive.Tagname (for example Interactive.Button). For more see Using createInteractive.
`js
// using with CSS-in-JS
import { Interactive, createInteractive } from 'react-interactive';
import { styled } from '@stitches/react';
import { Link } from 'react-router-dom';
const StyledButton = styled(Interactive.Button, {});
const StyledRouterLink = styled(createInteractive(Link), {});
const App = () => (
<>
>
);
`
`js
// using with another polymorphic component
import { Interactive, createInteractive } from 'react-interactive';
import { Link } from 'react-router-dom';
import { SomePolymorphicComponent } from '...';
const InteractiveRouterLink = createInteractive(Link);
const App = () => (
<>
>
);
`
---
as, onStateChange, children, disabled, interactive className, interactive style, useExtendedTouchActive, ref
---
Default value: "button"
React Interactive accepts a polymorphic as prop that can be a string representing a DOM element (e.g. "button, "a", "div", etc), or a React component (e.g. React Router's Link, etc).
`js
import { Interactive } from 'react-interactive';
import { Link } from 'react-router-dom';
const App = () => (
<>
My React Router Link
>
);
`
> Note that if as is a React component, then the component needs to pass through props to the element that it renders, including the ref prop using React.forwardRef(). Most libraries designed for composability do this by default, including React Router's component.
---
Default value: undefined
Callback function that is called each time the interactive state changes with both the current and previous interactive states (passed in as a single argument of the form { state, prevState }). See Reacting to interactive state changes.
---
Default value: undefined
If children is a ReactNode (anything that React can render, e.g. an Element, Fragment, string, boolean, null, etc) then it is passed through to React to render normally.
If children is a function then it is called with an object containing the current interactive state (note that the function must return a ReactNode that React can render). See Using the interactive state in children.
`js
import { Interactive } from 'react-interactive';
const App = () => (
{({ hover, active, focus }) => {
// hover: boolean,
// active: 'mouseActive' | 'touchActive' | 'keyActive' | false,
// focus: 'focusFromMouse' | 'focusFromTouch' | 'focusFromKey' | false,
// ...
// must return something that React can render
return Current state - active: ${active}, hover: ${hover}, focus: ${focus};
}}
);
`
---
Default value: false
Passing in a disabled prop is an easy way to temporarily disable a React Interactive component without changing the other props. When disabled is true:
- The disabledClassName and disabledStyle props will be used for styling the disabled component.disabled
- will be passed through to the DOM element if it is a
---
Default values: see below table
CSS classes that are added to the DOM element when in an interactive state. These are merged with the standard className prop which is always applied. See Styling with CSS.
| Prop | Default value |
| :------------------------ | :----------------- |
| hoverClassName | "hover" |activeClassName
| | "active" |mouseActiveClassName
| | "mouseActive" |touchActiveClassName
| | "touchActive" |keyActiveClassName
| | "keyActive" |focusClassName
| | "focus" |focusFromMouseClassName
| | "focusFromMouse" |focusFromTouchClassName
| | "focusFromTouch" |focusFromKeyClassName
| | "focusFromKey" |disabledClassName
| | "disabled" |
Note that:
- activeClassName is added when in any active state. This is in addition to the specific [input]ActiveClassName.focusClassName
- is added when in any focus state. This is in addition to the specific focusFrom[input]ClassName.disabledClassName
- is added when the disabled boolean prop is true, in which case none of the other interactive className props are applied.
---
Default values: undefined
Inline styles that are added to the DOM element when in an interactive state. These are merged with the standard style prop which is always applied. See Styling with inline styles.
Inline style prop list:
- hoverStyleactiveStyle
- mouseActiveStyle
- touchActiveStyle
- keyActiveStyle
- focusStyle
- focusFromMouseStyle
- focusFromTouchStyle
- focusFromKeyStyle
- disabledStyle
-
Style prop objects for each state are merged with the following precedence (last one wins):
- style prop (styles that are always applied)hoverStyle
- ===
- activeStyle
- [input]ActiveStyle
- focusStyle
- focusFrom[input]Style
- disabledStyle
- =OR=
- (when disabled, only the disabledStyle prop is merged with the style prop)
---
Default value: false
By default React Interactive only stays in the touchActive state while a click event (from the touch interaction) is still possible. To remain in the touchActive state for as long as the touch point is on the screen, pass in the useExtendedTouchActive prop. This can be useful for implementing functionality such as show on touchActive, long press, etc.
Note that anchor tags, , on touch devices have their own device/browser specific behavior for long press (context/callout menu, dragging, etc). If you need to disable the native behavior for long press of links you can:
- Set a onContextMenu event listener and call preventDefault(), to prevent the context menu from appearing.-webkit-touch-callout: none
- Set style to prevent the iOS "context menu" from appearing (iOS doesn't support contextmenu events).draggable="false"
- Set on the element (by passing it in as a prop).
---
Default value: undefined
React Interactive uses React.forwardRef() to forward the ref prop to the DOM element. Passing a ref prop to an Interactive component will return the DOM element that the Interactive component is rendered as.
React Interactive supports both object refs created with React.useRef() and callback refs created with React.useCallback().
---
React Interactive exports a createInteractive(as) function that returns a fully typed component with the as prop predefined.
This is the same as wrapping and passing through props like const MyWrappedInteractive = (props) => , but by the time you add ref forwarding and typing this can become verbose, and it may be something you need to do frequently in your app. So React Interactive provides a createInteractive convenience function that makes extending quick and easy.
Also, some commonly used DOM elements are available using Interactive.Tagname, for example Interactive.Button, to make things even easier (they are created using createInteractive('tagname')).
You can use components with the as prop predefined with JSX (instead of using the as prop), or you can use them with CSS-in-JS libraries and other polymorphic components to avoid as prop conflicts (this is where they are most useful). For more see Extending the component
.
`js
import { Interactive, createInteractive } from 'react-interactive';
import { Link } from 'react-router-dom';
// these are the already defined DOM elements
// for other DOM elements and components use createInteractive(as)
const InteractiveNav = createInteractive('nav');
const InteractiveRouterLink = createInteractive(Link);
`
---
React Interactive uses Event From under the hood to determine if browser events are from mouse, touch or key input. The eventFrom and setEventFrom functions are re-exported from Event From and can be useful when building apps with React Interactive.
The eventFrom(event) function takes a browser event and returns 1 of 3 strings indicating the input type that caused the browser event: 'mouse', 'touch', or 'key'. For example, this can be useful to determine what input type generated a click event.
`js
import * as React from 'react';
import { Interactive, eventFrom } from 'react-interactive';
const App = () => {
const handleClickEvent = React.useCallback((e) => {
switch (eventFrom(e)) {
case 'mouse':
// click event from mouse
break;
case 'touch':
// click event from touch
break;
case 'key':
// click event from key
break;
}
}, []);
return (
My Button
);
};
`
inputType: "mouse" | "touch" | "key"
This is useful when manually generating events. For example, when calling focus() on an component and you want it to enter the focusFromKey state.
`js
import * as React from 'react';
import { Interactive, setEventFrom } from 'react-interactive';
const App = () => {
const myButtonRef = React.useRef(null);
const focusInteractiveButton = React.useCallback(() => {
if (myButtonRef.current) {
// so the
setEventFrom('key');
myButtonRef.current.focus();
}
}, []);
return (
<>
ref={myButtonRef}
focusFromKeyStyle={{ outline: '2px solid green' }}
>
My Button
>
);
};
`
---
Basics, Exported types, onStateChange callback and children as a function, Props passed to
---
React Interactive is fully typed, including the polymorphic as prop. The props that an component accepts are a union of its own props and the props that the as prop accepts. Live TypeScript examples are available in TypeScriptExamples.tsx in the demo app.
`ts
import { Interactive } from 'react-interactive';
const App = () => (
href="https://rafgraph.dev" // TS knows href is a string b/c as="a"
>
My Link
);
`
---
`ts
type ActiveState = 'mouseActive' | 'touchActive' | 'keyActive' | false;
type FocusState = 'focusFromMouse' | 'focusFromTouch' | 'focusFromKey' | false;
// type for the state object used by React Interactive
// InteractiveState is passed to children (when children is a function)
interface InteractiveState {
hover: boolean;
active: ActiveState;
focus: FocusState;
}
// type used for the argument passed to the onStateChange callback
interface InteractiveStateChange {
state: InteractiveState;
prevState: InteractiveState;
}
// type used for props passed to an
type InteractiveProps
// type used when wrapping/extending an
type InteractiveExtendableProps
`
---
Also see TypeScriptExamples.tsx in the demo app.
`ts
import {
Interactive,
InteractiveState,
InteractiveStateChange,
} from 'react-interactive';
const App = () => {
// use the InteractiveStateChange type to type the argument
// passed to the onStateChange callback
const handleInteractiveStateChange = React.useCallback(
({ state, prevState }: InteractiveStateChange) => {
// ...
},
[],
);
// use the InteractiveState type to type the argument
// passed to children (when children is a function)
const childrenAsAFunction = React.useCallback(
({ hover, active, focus }: InteractiveState) => {
// ...
},
[],
);
return (
{childrenAsAFunction}
);
};
`
---
Sometimes you need to type the props object that is passed to an component, to do this use the type InteractiveProps. Also see TypeScriptExamples.tsx in the demo app.
`ts
import { Interactive, InteractiveProps } from 'react-interactive';
// props object passed to
// InteractiveProps includes types for as and ref
const propsForInteractiveButton: InteractiveProps<'button'> = {
as: 'button',
type: 'submit', // button specific prop
// ...
};
// for as={Component} use InteractiveProps
const propsForInteractiveAsComponent: InteractiveProps
as: Component,
// ...
};
const App = () => (
<>
>
);
`
---
When creating components that wrap an component, sometimes you want to extend the component and pass through props to . To do this use the type InteractiveExtendableProps. Also see TypeScriptExamples.tsx in the demo app.
> Note that if all you need to do is extend with a predefined as prop but without additional props and logic, use createInteractive(as) instead.`
>
> ts`
> import {
> Interactive,
> InteractiveExtendableProps,
> createInteractive,
> } from 'react-interactive';
> import { Link } from 'react-router-dom';
>
> // this works fine, but it's not necessary
> const InteractiveLink: React.VFC
> props,
> ) =>
>
> // do this instead, createInteractive also includes ref forwarding
> const InteractiveLink = createInteractive(Link);
>
`ts
import { Interactive, InteractiveExtendableProps } from 'react-interactive';
// the same props interface is used for wrapping with and without forwardRef
// note that InteractiveExtendableProps doesn't include as or ref props,
// when using forwardRef the ref prop type will be added by the forwardRef function
interface WrapperProps extends InteractiveExtendableProps<'button'> {
// OR extends InteractiveExtendableProps
additionalProp?: string;
}
// without ref
const WrapperWithoutRef: React.VFC
additionalProp,
...props
}) => {
// your logic here
return
};
// with ref
const WrapperWithRef = React.forwardRef<
HTMLButtonElement, // OR React.ElementRef
WrapperProps
>(({ additionalProp, ...props }, ref) => {
// your logic here
return
});
`
---
bugThe CSS sticky :hover bug on touch devices occurs when you tap an element that has a CSS :hover pseudo class. The :hover state sticks until you tap someplace else on the screen. This causes :hover styles to stick on touch devices and prevents proper styling of touch interactions (like native apps).
The reason for CSS sticky hover is that back in the early days of mobile the web relied heavily on hover menus, so on mobile you could tap to see the hover menu (it would stick until you tapped someplace else). Sites are generally no longer built this way, so now the sticky hover feature has become a bug.
React Interactive fixes the sticky hover bug by only entering the hover state from mouse input and creating a separate touchActive state for styling touch interactions.
---
- How do I manually set focus on an component?
- Pass in a ref prop and call focus() on the element (this is standard React). To enter a specific focusFrom[Input] state use setEventFrom.
- How do I disable an component?disabled
- Pass in a boolean prop.
- How do I use with another polymorphic component?createInteractive
- Use the function.touchActive
- The state is exited even though my finger is still on the button, how do I prevent this from happening?useExtendedTouchActive
- Pass in a boolean prop.
- Styling
- How do I style the interactive states with CSS?
- See Styling with CSS, and also this CodeSandbox for a live example.
- How do I style the interactive states with inline styles?
- See Styling with inline styles, and also this CodeSandbox for a live example.
- How do I use Styled Components with React Interactive?
- See this CodeSandbox for a live example.
- How do I use Stitches with React Interactive?
- See this CodeSandbox for a live example.
- TypeScript
- How do I use React Interactive with TypeScript?
- See Using with TypeScript.
- What types are exported from React Interactive?
- See Exported types from React Interactive.
- How do I extend an component including the TypeScript prop types?
- See Typing components that wrap