question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Passing style to child component without using global styles

See original GitHub issue

I’ve got a child component, let’s call it Link.

This is the code for that:

import React from 'react';
import Link from 'next/link';

export default class CustomLink extends React.Component {
  render() {
    if (this.props.onClick) {
      return (
        <span className={this.props.className} onClick={this.props.onClick}>
          {this.props.children}
        </span>
      );
    }

    return (
      <Link
        href={this.props.href}
        params={this.props.params}
        as={this.props.as}>
        <a className={this.props.className}>{this.props.children}</a>
      </Link>
    );
  }
}

Now say that I import that component within another component, called Header

The code for that is:

import Link from './Link';

export default class Header extends React.Component {
  render() {
    return (
      <div className="root">
        <style jsx>{`
          .link {
            background-color: #fff;
            border-bottom: 1px solid #eee;
            color: #222;
          }
          `}
        </style>
       <Link className="link">Click me</Link>
    );
  }
}

However, unless I use <style jsx global>, then unfortunately the style doesn’t get passed to the child component.

I think a related issue is #197, but I haven’t seen any outcome or update on that issue since June.

Is there a solution to fixing this issue?

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:17
  • Comments:12 (4 by maintainers)

github_iconTop GitHub Comments

22reactions
giuseppegcommented, Sep 6, 2017

what if Link comes from some npm module (e.g. a UI library), and I don’t have control over it?

That’s the main point, we’d have to figure out a way to control styles from the parent because passing a prop (eg. className) is an half baked solution that won’t work all the times.

As for now the best way to do this is to use .someSelectorInTheParentComponent > :global() from within the parent component eg.

<div className="root">
    <Link />
    <style jsx>{`
        .root > :global(a) { font-size: 60px }
     `}</style>
</div>
14reactions
a-ignatov-parccommented, Dec 12, 2017

I’ve found a better workaround for this issue.

function HighlightedLink(props) {
  const {
    theme,
    children,
    ...otherProps,
  } = props;

  /**
   * `scope` element is needed to properly parse `style` element
   * and it could be any DOM element you want.
   */
  const scope = resolveScopedStyles((
    <scope>
      <style jsx>{`
        .link {
          background: ${theme.background};
        }
      `}</style>
    </scope>
  ));

  return (
    <Link
      {...otherProps}
      className={scope.wrapClassNames('link')}
    >
      {children}
      <scope.styles />
    </Link>
  );
}

function resolveScopedStyles(scope) {
  return {
    className: scope.props.className,
    styles: () => scope.props.children,
    wrapClassNames: (...classNames) => [scope.props.className, ...classNames].filter(Boolean).join(' '),
  };
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

How to style child components from parent component's CSS ...
use @Component to add css class to host element and set encapsulation to none. Then reference that class which was added to the...
Read more >
How to Style Child Component From Parent In Angular - Medium
Instead of expose all the styles of that component to have a global effect, a slightly better solution is to use the ::ng-deep...
Read more >
Styling child component from parent in Angular
The simplest solution for this problem is to set the encapsulation property to ViewEncapsulation. None in the component.
Read more >
"proper" way to style child elements via CSS? : r/angular - Reddit
You've got several options: Add the styles to the global stylesheet and make them unique enough to not conflict. Add another stylesheet and ......
Read more >
Component styles - Angular
While Angular's emulated style encapsulation prevents styles from escaping a component, it does not prevent global CSS from affecting the entire page.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found