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

DOM Element Refs #41

Closed
conradz opened this issue Jun 28, 2017 · 4 comments
Closed

DOM Element Refs #41

conradz opened this issue Jun 28, 2017 · 4 comments

Comments

@conradz
Copy link

conradz commented Jun 28, 2017

Currently it doesn't look like there is any way to get a reference to the DOM node. Setting a ref prop on the component always sets the ref to null (I think this is because it uses a functional component; React doesn't support refs on these). Styled-components allows a innerRef property for this. I looked at implementing this in emotion, but it looks like it might be a bit more complicated than I thought at first since you can wrap other styled components, not just DOM nodes.

My use-case for this is to allow focusing a DOM node based on keyboard events. For this I need a reference to the actual DOM node to call .focus(), either using findDOMNode or a ref on the actual DOM node. Any help or pointers are greatly appreciated.

@conradz
Copy link
Author

conradz commented Jun 28, 2017

I did find that you can wrap it in a Component class that works around this:

const Input = styled('input')`font-size: 16px`
class InputWrapper extends React.Component {
  render () { return <Input {...this.props} />; }
  focus () { ReactDOM.findDOMNode(this).focus(); }
}

@tkh44
Copy link
Member

tkh44 commented Jun 28, 2017

Yes this is a needed feature.

We need to extend the api for styled to accept an innerRef option.

@CompuIves
Copy link
Contributor

I tried my hand at it: #43

@tkh44
Copy link
Member

tkh44 commented Jun 28, 2017

Closed in #43

@tkh44 tkh44 closed this as completed Jun 28, 2017
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

3 participants