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

Link as a StyledComponent #799

Closed
edygar opened this issue Jan 16, 2017 · 8 comments
Closed

Link as a StyledComponent #799

edygar opened this issue Jan 16, 2017 · 8 comments

Comments

@edygar
Copy link

edygar commented Jan 16, 2017

The way Link is implemented right now, it's impossible to compose styles using styled-components without generating <a><a></a></a>.

#241 would solve. Any updates on turning Link a behavior decorator only?

@timneutkens
Copy link
Member

@edygar you mean <Link href="/"><a>content</a></Link> right?

Deprecation message has just been added in #797 👍
We're only going to support the explicit given example: <Link href="/"><a>content</a></Link>

Does this solve your question?

@jlcarvalho
Copy link

jlcarvalho commented Jan 16, 2017

@timneutkens in this case the a don't get the styles.

import Link from 'next/prefetch'
import styled from 'styled-components'

const MenuItem = styled.li`
  margin: 0 10px;
  list-style: none;
`;

const MenuLink = styled(Link)`
  font-weight: 700;
  line-height: 2.88;
`

export default () => (
  <MenuItem>
    <MenuLink href='/signin'>
      <a>Sign In</a>
    </MenuLink>
  <MenuItem>
)

Result:

<li class="cjJDVk" data-reactid="8">
  <a href="/signin" data-reactid="9">Sign In</a>
</li>

@impronunciable
Copy link
Contributor

impronunciable commented Jan 16, 2017

@jlcarvalho Yes, you should style the <a>:

import Link from 'next/prefetch'
import styled from 'styled-components'

const MenuLink = styled.a`
  font-weight: 700;
  line-height: 2.88;
`

export default () => (
  <Link href='/signin'>
    <MenuLink>Sign In</MenuLink>
  </Link>
)

@jlcarvalho
Copy link

jlcarvalho commented Jan 16, 2017

@impronunciable I tried, but the output was:

<li class="ePmGlD">
  <a href="/signin">
    <a class="jbHZic">Sign In</a>
  </a>
</li>

@impronunciable
Copy link
Contributor

Oh, I see. That's because the styled component is not an anchor. We are going to deprecate this behavior but I don't see a fix for this in the meanwhile

@edygar
Copy link
Author

edygar commented Jan 16, 2017

Exactly as @jlcarvalho said.

@impronunciable, why does it matters for Link the Element.type of its first child? I think it could only provide the click behavior. Currently it's only discouraging composability.

@timneutkens
Copy link
Member

Please see #816 👍

@chhuang
Copy link

chhuang commented Apr 10, 2017

I tried this and it seems to be working fine?

import Link from 'next/link';
import styled from 'styled-components';

const StyledLink = styled.a`
  color: red;
  background: blue;
`;

export default ({ children }) => (  
  <Link>
    <StyledLink>{ children }</StyledLink>
  </Link>
);

@lock lock bot locked as resolved and limited conversation to collaborators May 11, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants