material-ui icon indicating copy to clipboard operation
material-ui copied to clipboard

Button wrapped with Link from react-router does not work in Safari

Open mariusk opened this issue 11 years ago • 38 comments

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 avatar Jun 15 '15 13:06 mariusk

@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.

mairh avatar Jun 15 '15 14:06 mairh

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.

mariusk avatar Jun 15 '15 14:06 mariusk

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"/>} />

hai-cea avatar Jun 15 '15 15:06 hai-cea

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.

mariusk avatar Jun 15 '15 16:06 mariusk

Actually with #846, you'll be able to do:

<FlatButton>
  <Link to="/login/" />
</FlatButton>

hai-cea avatar Jun 15 '15 17:06 hai-cea

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>

mariusk avatar Jun 15 '15 17:06 mariusk

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 avatar Jun 16 '15 06:06 mariusk

@mariusk You should be able to do this now:

<FlatButton
  containerElement={<Link to="/login" />}
  linkButton={true}
  label={l.l('no', 'Sign in')}/>

hai-cea avatar Jun 17 '15 05:06 hai-cea

That looks and works perfect, thanks, great work!

mariusk avatar Jun 17 '15 06:06 mariusk

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?

gmaclennan avatar Jun 21 '15 19:06 gmaclennan

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>?

antoinerousseau avatar Jan 28 '16 04:01 antoinerousseau

@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

sovanna avatar Jan 31 '16 22:01 sovanna

containerElement is not documented :(

aolu11 avatar Sep 06 '16 10:09 aolu11

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?

dominikbulaj avatar Sep 16 '16 19:09 dominikbulaj

Doesn't work any longer

Warning: Unknown prop linkButton on tag. Remove this prop from the element.

link button error - google chrome 2017-01-28 21 05 23

@wzup just remove the linkButton={true}

waclock avatar Feb 06 '17 13:02 waclock

@wzup If you write your code like this without linkButton prop, it should work.

<FlatButton
  containerElement={<Link to="/login" />}
/>

Shahrukh-Zindani avatar Feb 15 '17 20:02 Shahrukh-Zindani

@hai-cea , hello,

how can i make a function on login page [say modal function] to popup,in the above code once it navigates after button click

RakeshBKrishnamurthy avatar Mar 20 '17 11:03 RakeshBKrishnamurthy

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"/>

StephanBijzitter avatar Apr 02 '17 12:04 StephanBijzitter