Button wrapped with Link from react-router does not work in Safari
React-router uses ... type components to create SPA-type navigation events. If I wrap any kind of material-ui button with Link, it works fine in Chrome, but not in Safari. In Safari, no navigation events happen. A workaround is to add an explicit onTouchTap handler on the button itself, but I believe this is some kind of bug.
@mariusk Could you please tell me how did you add the Link component from react-router to Button component. For example, if I do something like the following it doesn't work.
let editLink = <Link to="editTopic"/>;
<IconButton
linkButton={true}
onTouchTap={editLink}
tooltip="Edit forum">
<i className="material-icons">edit</i>
</IconButton>
I notice that if I add linkButton={true} I need to add the route to href element, but react router Link generates the anchor tag by default.
Here's the actual code:
<Link to="/login"><FlatButton onTouchTap={this.clickHandler}>{l.l('no', 'Sign in')}</FlatButton></Link>
Without the onTouchTap handler, it works in Chrome but not in Safari. With the explicit handler (which again calls react-router's transitionTo(...) it also works in safari.
This might be because FlatButton generates a <button /> element and I believe <Link /> generates an <a /> element. So you'll have:
<a><button /></a>
One solution that I can think of is to change enhanced-button to accept an element prop that will allow users to customize the container element. So you'll be able to do:
<FlatButton containerElement={<Link to="/login"/>} />
You're right; button inside an a element is not valid html5. Another possible solution would be to have the button styles available for a elements (in addition to button), and then simple style it with className.
Actually with #846, you'll be able to do:
<FlatButton>
<Link to="/login/" />
</FlatButton>
I just tested #846. It broke the styling (button text is white on white), and the link isn't active (doesn't work). Here's the actual code I tested:
<FlatButton><Link to="/login">{l.l('no', 'Sign in')}</Link></FlatButton>
In addition to the color issue (which was caused by my own styling), and the fact that the link still does not work, there's also a focus issue. When hovering over the embedded a link, only the link parts gets highlighted, not the whole button.
@mariusk You should be able to do this now:
<FlatButton
containerElement={<Link to="/login" />}
linkButton={true}
label={l.l('no', 'Sign in')}/>
That looks and works perfect, thanks, great work!
When I switch to this I am getting EventEmitter memory leak warnings. I'm not sure whether it is the click handlers on <Link> or those on <EnhancedButton> which are not being removed properly? Maybe <Link> is not being properly unmounted?
Looks like it's now working even without linkButton={true}, right? When the button has Link for containerElement it is turned into a <a> element rather than a <button>?
@hai-cea nice, thx!
@antoinerousseau yes, turn into <a>, broke the one style... the text originally set to text-align: center 's changed into left..as its now a <a> element
containerElement is not documented :(
I see that this setup (e.g. RaisedButton width containerElement={<Link to="/register>} has CSS issue. Ripple effect is not firing properly, Actually it's firing but with so much delay that I nearly can't see it (until I tap longer such button). I'm using v0.15.4.
Any hint how to speed it up?
@wzup just remove the linkButton={true}
@wzup If you write your code like this without linkButton prop, it should work.
<FlatButton
containerElement={<Link to="/login" />}
/>
@hai-cea , hello,
Pardon the bump but the following still generates a warning on 0.17:
<RaisedButton
label="Sign In"
containerElement={<Link to="/login" />}
/>
Generates the following:
Warning: Unknown prop onTouchTap on tag. Remove this prop from the element. For details, see https://fb.me/react-unknown-prop
I had the same problem, so I created a reusable component that works for me.
import React from 'react';
import {Component, PropTypes} from 'react';
import FlatButton from 'material-ui/FlatButton';
export default class FlatLinkButton extends Component {
static propTypes = {
...FlatButton.propTypes,
to: PropTypes.string.isRequired,
link: PropTypes.func.isRequired,
label: PropTypes.node.isRequired,
};
render() {
const style = Object.assign({
color: 'white',
marginTop: '7px'
}, this.props.style);
const props = {
...this.props,
style,
};
delete props.to;
delete props.link;
const Link = this.props.link;
return (
<Link to={this.props.to}>
<FlatButton {...props}/>
</Link>
);
}
}
Usage:
import {Link} from 'react-router';
import FlatLinkButton from './FlatLinkButton';
<FlatLinkButton to="/login" link={Link} label="Login"/>
