w3c / csswg-drafts

CSS Working Group Editor Drafts
https://drafts.csswg.org/
Other
4.48k stars 662 forks source link

[css-contain-3] Nesting Context (side effect / enhancement) #7209

Open jakob-e opened 2 years ago

jakob-e commented 2 years ago

Based on the current implementation in Chrome Canary and Safari TP Container Queries seems to only need a property name – allowing them to be used to expose nesting context.

  /* this works */
  .container-a {
    container-name: A;
    container-type: inline-size;
  }

  @container A (inline-size) {
    .child { color: orange; }
  }

IMO it makes a lot of sense to use Container Queries to handle this scenario (child styling without a direct reference to the container selector(s)).

If possible it would be nice if container-name could be used without having to specify container-type and query

  /* simplified syntax (suggestion) */
  .container-a {
    container-name: A;
  }

  @container A {
    .child { color: orange; }
  }

Codepen Example

I hope it makes sense and that I haven't overlooked existing issues Thank you for your hard work – it is highly appreciated 😃

andruud commented 2 years ago

Maybe this is a job for @scope instead? https://drafts.csswg.org/css-cascade-6/#scope-atrule

jakob-e commented 2 years ago

Maybe this is a job for @scope instead? https://drafts.csswg.org/css-cascade-6/#scope-atrule

I'm thinking it more as a reference to the container selector(s) using a simplified syntax based on the current implementation

.sidebar-left, 
#sidebar-right {
   container-name: sidebar;
}

/* nested inside .sidebar-left or #sidebar-right */
@container sidebar {
  .component { color: tomato; }
}
tabatkins commented 2 years ago

If I'm reading the request correctly, I think this is just asking for the CSS Nesting spec, right?

jakob-e commented 2 years ago

If I'm reading the request correctly, I think this is just asking for the CSS Nesting spec, right?

No, it is more of a reference - I'm not sure how to explain it - maybe this demo will help clarify Here is a demo link

Chrome Canary with container queries enabled chrome://flags/#enable-container-queries or Safari TP

tabatkins commented 2 years ago

Right, it's like Sass's @extend feature that lets you package a selector into a ref. What I'm saying is that, for the most part, Nesting gives you the same benefits, just without the potential for indirection across a stylesheet.

jakob-e commented 2 years ago

I'm not sure what you mean by

potential for indirection across a stylesheet

What I'm talking about is allowing to omit parts of a syntax in the _Container Query_ implementation that is already being rolled out (not a new feature).

As shown in the demo you can use CQ's to detect if a selector is a child of a container. But if used for that purpose (side effect) alone the syntax feels a bit hackish.

My suggestion is that – if you omit the parts shown below as comments - the type and query would be implicit. I know inline-size is not the correct type it was just what I found to be working

.container {
   container-name: MyContainer;
   /* container-type: inline-size; */
} 

@container MyContainer /* (inline-size) */ {
   .child { color: orange; }
}

I'm sorry for being bad at explaining myself – please bare with me 🙏🏻🤗

tabatkins commented 2 years ago

The "indirection across a stylesheet" means that you can define a container in one spot, and then rely on the container name somewhere else to condition another selector; as opposed to Nesting, where the outer selector is directly wrapped around the inner selector with no indirection. You can even define a container in a different stylesheet entirely!

But that indirection is the only benefit this (admittedly hacky) usage brings over Nesting. They're otherwise doing the same thing.

jakob-e commented 2 years ago

True, but I feel this is similar to CSSVars – a trade-off for having better composition options (structure).

mirisuzanne commented 1 year ago

This is a use-case that I've considered as well. I think there might be use-cases for simply checking that you are in a named container, without additional query info. There's a lot of overlap with the use-cases for style queries or higher level custom properties – checking to see if you are in a particular style context, and adjusting to match it. This would basically be syntax sugar for a style query of the container-name property, which in some cases could be used instead of creating a new custom property to query.

To summarize: