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

Selecting children by class name #12

Closed
jsakas opened this issue Aug 18, 2021 · 10 comments
Closed

Selecting children by class name #12

jsakas opened this issue Aug 18, 2021 · 10 comments

Comments

@jsakas
Copy link
Contributor

jsakas commented Aug 18, 2021

Hello, I am trying to figure out how to select nested children by class name using this library. Below is an example of how we did it with JSS:

  el1: {
    '&:hover $el2': {
      display: 'block',
    },
  },
  el2: {
    display: 'none'
  },

Is this currently supported? The docs say:

tss-react unlike jss-react doesn't support the $ syntax, but you'll see. It isn't needed.

But don't clarify how we would do the above.

@garronej
Copy link
Owner

Hi @jsakas,
Sorry, it's an oversight of my part.
I wasn't encline to implement this API because it's hard to make it type safe.
Now I understand that it's a must have.
I will implement a feature to cover this use case, I can't tell you exactly when though.

garronej added a commit that referenced this issue Aug 18, 2021
@garronej
Copy link
Owner

I think I can come up with an API like that:

const useStyles = makeStyles<{ color: "red" | "blue" }>()(
    (theme, { color }, classes) => ({
          "el1": {
              [`&:hover .${classes.el2}`]: {
                  "display": "block"
              },
          },
          "el2": {
               "display": "none"
          },
    })
);

Any though?

@jsakas
Copy link
Contributor Author

jsakas commented Aug 18, 2021

Any though?

Yes, I think that would be a great alternative to the jss syntax.

@jsakas
Copy link
Contributor Author

jsakas commented Aug 18, 2021

I will implement a feature to cover this use case, I can't tell you exactly when though.

This seems pretty critical to anyone who is migrating from Material UI v4 to v5 but can't move to styled components syntax yet.

I'm trying to address myself on a fork but it seems like the proposed API might not be possible due to the order of operations in the library.

@garronej
Copy link
Owner

garronej commented Aug 18, 2021

This seems pretty critical to anyone who is migrating from Material UI v4 to v5 but can't move to styled components syntax yet.

Agreed, this needs to be done ASAP. I will try to free up time in the coming days.

I'm trying to address myself on a fork but it seems like the proposed API might not be possible due to the order of operations in the library.

To implement the proposed API I think we will need a preflight to get [ "el1", "el2" ] or maybe we could make it work with proxies...

@garronej
Copy link
Owner

I will have a look tonight. Maybe I can get it done in a couple of hours...

@jsakas
Copy link
Contributor Author

jsakas commented Aug 18, 2021

I have started on the work but have been unable to get it to work properly with typescript typings. The classes object keeps coming back as Record<never, string> and I am unsure why. See the PR here: #13

@garronej
Copy link
Owner

garronej commented Aug 18, 2021

dollar_syntax_tss

Nice!

@garronej
Copy link
Owner

garronej commented Aug 18, 2021

const useStyles = makeStyles()((theme, _params, css) => {

	const child = {
		"background": "blue",
		"border": "1px solid black"
	};

	return {
		"parent": {
			"border": "1px solid black",
			"padding": 30,
			[`&:hover .${css(child)}`]: {
				"background": "red",
			}
		},
		child
	};
});
//...

<div className={classes.parent}>
	<div className={classes.child}>
		Background should turn red when mouse is hover the parent.
        </div>
</div>

@garronej
Copy link
Owner

Hi @jsakas,
I am sorry I had to update the API for nested selector. The approach based on css was broken.
https://github.com/garronej/tss-react#selecting-children-by-class-name

gitbook-com bot pushed a commit that referenced this issue Jan 22, 2022
gitbook-com bot pushed a commit that referenced this issue Jul 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants