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.

Feature Request: Provide `themeName` prop for components wrapped by `themr`

See original GitHub issue

Having to deal with the style object generated by module bundlers is typically unfriendly, for all the reasons outlined in react-css-modules#whats-the-problem.

It’d be nice if styling was done through a similar approach to what react-css-modules provides by way of a themeName prop that gets inspected by the HOC and is automatically injected into that children component’s classNames. Having a similar API also provides more hints to the user as to which one they should be wrapping their components in: if its styles are meant to be overriden or injected, use themr, otherwise use react-css-modules. However, it’s a little bit awkward in that themr could almost be seen as a superset of react-css-modules, but I think there’s something to be gained by limiting the themeability of a component through react-css-modules and I see at least one clear use case where I’d use both together – inline child components:

import React from 'react';
import CSSModules from 'react-css-modules';
import { themr } from 'react-css-themr';
import List from './List';
import theme from './table.css';

class CustomList extends React.Component {
    render () {
        let itemTemplate = CSSModules((name) => (
            <li styleName='item-template'>{name}</li>;
        ), this.props.theme); // or even subset of theme (ie. { 'item-template': this.props.theme['item-template'] })

        return <List themeName="list" itemTemplate={itemTemplate} />;
    }
}

export default themr("ListComponent", theme)(CustomList);
// As an aside, it'd also be nice to expose a normal function operator,
// see https://github.com/gajus/react-css-modules/blob/master/src/index.js#L47

Thoughts? Also pulling in @gajus since he might have some thoughts to share.

I’ll probably start working on this soon since I’m finally getting around to using this (awesome work btw, @javivelasco, a lot of this makes a lot of sense and solidified a lot of the ideas I had wanted to implement myself!) but the similarities to react-css-modules means I’ll likely pull a lot from that repo or generalize it into some generic style utils.

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:15 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
javivelascocommented, Jun 22, 2016

Hi guys! I couldn’t read the whole thread yet but I wanted to drop a quick comment before going to sleep. First of all, congrats @gajus for the project, I looked at it long ago and it’s a great work, I’m not familiar with the current state though. Thanks @SohKai for your kind words! 😃

I’ve written this small module to be used with react-toolbox. It’s quite difficult to customize components so what I wanted to do at first was just to provide a way to add classes to the component following an API.

For example, imagine a menu with a pointer that can be positioned in multiple places depending on some props. Typically the pointer will be done with pseudoelements and, in case you want to customize the background, for that specific class what you’d do is to change the border-color. Just one property. That’s way instead of provide new styles for the menu, you just add classes using the theme property. The HOC appends them for you. I don’t know if nowadays it’s possible to do this in react-css-modules, but if it’s I couldn’t figure out before writing this.

Another example is to provide the whole theme in a single place. In react toolbox you should be able to import a Raw component and I wanted to give the ability to customize a full application without changing toolbox imports. This means you’d be importing a RAW toolbox component and then providing the theme somewhere else. The best way to do this without spreading props through the tree was by using contexts. So I came up with this idea of setting an object with the relation between component ids and theme objects.

More or less those are the ideas behind. If all of this can be done I’d happily move to react-css-modules 😃 Tomorrow I’ll read better the messages and try to be more accurate in my answers. Thanks for your time checking on this lib!

0reactions
javivelascocommented, Aug 23, 2016

Ok! If there is something we can do to improve integration just reopen. It looks like the lib is gaining popularity and I’d love to make it as good as possible 😃 Thanks and sorry again for my late response @sohkai !!

Read more comments on GitHub >

github_iconTop Results From Across the Web

[2.0] RFC: docusaurus swizzle --wrap/copy #5380 - GitHub
Feature We have a feature to wrap existing theme components: https://docusaurus.io/docs/using-themes#wrapping-theme-components import ...
Read more >
Advanced Usage - styled-components
styled-components has full theming support by exporting a <ThemeProvider> wrapper component. This component provides a theme to all React components ...
Read more >
Swizzling - Docusaurus
Wrapping : creates a wrapper around the original theme component, which you can enhance. Why is it called swizzling?
Read more >
Build a React theme switcher app with styled-components
In this tutorial, we demonstrate how to build a custom theme switcher for a Game of Thrones-themed React app using styled-components.
Read more >
How To Create Wrapper Components in React with Props
To create wrapper components, you'll first learn to use the rest and spread operators to collect unused props to pass down to nested...
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