Skip to content

Nested Property for nested rules/@rule #41

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

Merged
merged 17 commits into from
Dec 2, 2016
Merged

Conversation

notoriousb1t
Copy link
Contributor

@notoriousb1t notoriousb1t commented Nov 29, 2016

This PR aims to solve this issue #40 . If accepted, it should be considered a minor/major change since it breaks nesting selectors next to properties.

It adds the following "nested" property to allow nesting selectors:

style({
  nested: {
    '@media screen': {
        color: 'red'
     }
  }
})

It adds shorthands for simple pseudo-selectors so they can still be used without going into a "nested" property.

style({
    color: 'blue',
    '&:active': {
      color: 'red' 
    }
});

And adds several CSS Properties needed to compile and pass the existing tests

@basarat
Copy link
Contributor

basarat commented Nov 29, 2016

It adds shorthands for simple pseudo-selectors so they can still be used without going into a "nested" property.

I feel like this will only result in confusing and difficult to debug errors. Although I did try to do the same (there were a few in NestedCSSProperties) I found people at work and in the wild e.g. https://github.com/staltz/matrixmultiplication.xyz/blob/8f1d761f5e49e2ccd92a1760b07ec6c7a72fbd9e/src/App/styles.ts#L39-L41 using all sorts of selectors with opinions on formatting ... more than the ones I used.

I'll remove that so there is only one way to do things without special cases being documented / maintained elsewhere.

it should be considered a minor/major change since it breaks nesting selectors next to properties.

This has been the biggest concern at the back of my head and the reason I didn't go with v1 yet. So happy to go v1 once we are settled on this :)

@notoriousb1t
Copy link
Contributor Author

I can't argue with the pseudo-selector thing. I put that in there as an attempt to break as little code as possible, but I can see how it would be confusing if someone did that and expected '& > *' to work.

I also added a special function for fontFace since it has properties directly and some of them appear to be specific to declaring fonts.

@basarat
Copy link
Contributor

basarat commented Nov 29, 2016

I also added a special function for fontFace

Looks good. Thanks. Its a common enough task to deserve its function just like we have for keyframes 🌹

@basarat
Copy link
Contributor

basarat commented Nov 29, 2016

@notoriousb1t Just need a 👍 from you. I'll merge + deploy + write release notes + update website 🌹

@basarat
Copy link
Contributor

basarat commented Nov 29, 2016

Hmm.. It doesn't error on a typo still

image

edit sample:

style({
        nested: {
          '&:hover': {
            typo: 'red',
            backgroundColor: hsl(0, '100%', '50%'),
          }
        }
      });

@basarat
Copy link
Contributor

basarat commented Nov 29, 2016

Suspect TypeScript type system is getting confused by circular types. Will try and narrow down an issue report 🌹

@basarat
Copy link
Contributor

basarat commented Nov 29, 2016

I created an issue microsoft/TypeScript#12566 . Suspect the answer will be no.

Also had a discussion with colleagues at work and they prefer a nested() function as its easier to write without erroring

declare var style: any; 
declare var nested: any;

// A 
style({
    color: 'grey',
    nested: {
        '&:hover': {
            color: 'red'
        }
    }
})

// B
style(
    { color: 'grey' },
    nested('&:hover', {
        color: 'red'
    })
)

Had people struggle with A and they would write stuff like the following failing to manage brackets correct / and forgetting to write the selector.

style({
    color: 'grey',
    nested: {
       color: 'red'
    }
})

also we wouldn't need to teach people {nested:{[code]:style}} computed property syntax as nested(code,style) is something that people understand from ES5.

I'll work on that soon 🌹

@notoriousb1t
Copy link
Contributor Author

There is no reason we can't do both 😉

function nested(code, props) {
   return {
      nested: {
         [code]: props
      }
   }
}

style(
    { color: 'grey' },
    nested('&:hover', {
        color: 'red'
    })
)

@basarat
Copy link
Contributor

basarat commented Nov 30, 2016

Will hold off till TS teams comes back on the error 🌹

@notoriousb1t
Copy link
Contributor Author

Yeah. Trying to replicate CSS in a formal type system has resulted in some pretty interesting types. I think we might be pushing the boundaries a little bit here.

@basarat
Copy link
Contributor

basarat commented Dec 1, 2016

So the code we have works fine if strictNullChecks is false:

image

@vladima did the analysis microsoft/TypeScript#12584 and already posted a fix for strictNullChecks as well : microsoft/TypeScript#12595

@notoriousb1t I say we can merge this. Hurray for {nested:{'&:hover':obj}}. Thanks for your patience and standing your ground on excellent API design ❤️

@notoriousb1t
Copy link
Contributor Author

Hurray! 😄

@basarat basarat merged commit 6409dc9 into master Dec 2, 2016
@basarat basarat deleted the css-props-experiement branch December 2, 2016 07:53
@basarat
Copy link
Contributor

basarat commented Dec 2, 2016

The changes are covered here : #43. Will try to wait for microsoft/TypeScript#12595 to be merged and think through anything else that needs to be done before 1.0 🌹

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

Successfully merging this pull request may close these issues.

3 participants