Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Force ripple via props? #24

Open
mshwery opened this issue Jul 10, 2016 · 5 comments
Open

Force ripple via props? #24

mshwery opened this issue Jul 10, 2016 · 5 comments

Comments

@mshwery
Copy link

mshwery commented Jul 10, 2016

I was hoping I could achieve forcing the ripple to appear (centered as requested in #22) programmatically. This would enable one to use the focus state on a button to have the ripple appear and then disappear on blur.

@nhunzaker
Copy link
Contributor

I like this. What kind of API would you like to see here? One idea is to use refs and methods on the Ink component itself:

React.createClass({
  triggerRipple() {
    // I'm thinking a percentage value between 0 and 1
    // to prevent the need to know about the dimensions of the canvas for x/y
    this.refs.ink.trigger({ x: 0.5, y: 0.5 })
  },
  render() {
    return (
      <button onMouseOver={ this.triggerRiple }><Ink ref="ink" /></button>
    )
  }
})

Hmm. Maybe even a hold/release mechanism, something like:

const ripple = this.refs.ink.press({ x: 0.5, y: 0.5 })

setTimeout(function() {
  ripple.release()
}, 500)

What would work best for your app?

@mshwery
Copy link
Author

mshwery commented Jul 11, 2016

I haven't studied the internals of react-ink enough to know what best fits the current structure but IMO an approach like this would be best:

import React from 'react';
import Ink from 'react-ink';

const Button = (props) => (
  <button>
    <Ink touched={true}/>
  </button>
);

You could imagine that a boolean prop like touched would force the ripple to appear. The ripple internally could handle prop changes to transition from having touched={true} to touched={false}.

One could also assume centered coordinates for the ripple if not are specified, which I think might be the common use case.

To elaborate on the above example, here's how I would use it:

import React from 'react';
import Ink from 'react-ink';

class Button extends React.Component {
  constructor(props) {
    super(props);
    this.onBlur = this.onBlur.bind(this);
    this.onFocus = this.onFocus.bind(this);
  }

  onBlur(event) {
    this.setState({ hasFocus: false });
  }

  onFocus(event) {
    this.setState({ hasFocus: true });
  }

  render() {
    const inkProps = {};

    if (this.state.hasFocus) {
      inkProps.touched = this.state.hasFocus;
    }

    return (
      <button onBlur={this.onBlur} onFocus={this.onFocus}>
        <Ink {...inkProps}/>
      </button>
    );
  }
}

This example will set the touched prop to true only when the button has focus. When it no longer has focus the entire prop is removed (as opposed to passing false) so that normal clicking behavior will resume on Ink.

@mshwery
Copy link
Author

mshwery commented Jul 11, 2016

Your suggestion might be easier to work – but I've traditionally avoid externalizing methods on components.

@nhunzaker
Copy link
Contributor

@mshwery Sorry for the delay. I like that better myself.

@nhunzaker
Copy link
Contributor

And I like to send my comments too early!

I'm happy to crank on this, however I probably won't be able to get to it until next week.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants