Coder Social home page Coder Social logo

buffer-redux-hover's Introduction

buffer-redux-hover

Build Status

Keep react component hover state in redux

Usage

Combine the reducer under the hover state tree

import { combineReducers } from 'redux';
import { reducer } from '@bufferapp/redux-hover';

const app = combineReducers({
  hover: reducer, // important to have this under the hover state tree
});

export default app;

Create a component that has a hovered, onMouseEnter and onMouseLeave prop.

import React, { PropTypes } from 'react';

const MyComponent = ({
  hovered, // managed by redux-hover
  onMouseEnter,
  onMouseLeave,
}) =>
  <div
    onMouseEnter={onMouseEnter}
    onMouseLeave={onMouseLeave}
    style={{ background: hovered ? 'green' : 'red' }}
  >
    Hover This
  </div>;

MyComponent.propTypes = {
  hovered: PropTypes.bool,
  onMouseEnter: PropTypes.func,
  onMouseLeave: PropTypes.func,
};

export default TestComponent;

Wrap MyComponent with the Hoverable component. Make sure you set and id.

import React from 'react';
import { Hoverable } from '@bufferapp/redux-hover';
import MyComponent from './MyComponent';

const App = () =>
  <div>
    <Hoverable id={'myComponent'}>
      <MyComponent />
    </Hoverable>
    <Hoverable id={'myOtherComponent'}>
      <MyComponent />
    </Hoverable>
  </div>;

export default App;

Notes

hovered prop

the hovered prop is set to true on MyComponent when the mouse is hovering it. Otherwise it's set to false.

Choosing Id's

As long as id's are different, they'll be independently hoverable. The above example sets the strings manually, but you could also use a uuid() too.

This also means that ids with the same value will all get the hover state applied when any of them are hovered.

MyComponent is cloned with React.clone

This keeps the number of elements on the page minimal but adds a little overhead to clone the hoverable component.

buffer-redux-hover's People

Contributors

hharnisc avatar

Watchers

 avatar  avatar  avatar

Forkers

markchipman

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.