-
Notifications
You must be signed in to change notification settings - Fork 12k
Components should create a display:block
style by default
#12244
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
Comments
From @Meligy on September 8, 2018 1:3 This issue root is in how Angular keeps the component custom elements with their custom tag names in the rendered DOM, unlike other frameworks that omit these tags from output like React. The design decision might not be bad overall, and is probably here to stay, but a problematic effect of this design is that these component elements get the It's an instance where lack of action (no Since it's not even This is where a documentation addition might help, also maybe using the Angular CLI default component schematic to add |
From @SanderElias on September 8, 2018 4:18 @Meligy I'm aware of the history of this issue. I agree with the design choices. Still, Angular is in a position where we can change this browser default. The browsers can't do this, but Angular can. Angular is in a unique position, that can make the The cognitive load of Angular is already pretty high. Changing this would lower it a bit. |
From @trotyl on September 8, 2018 5:0 @SanderElias I don't think this is about component, but only for custom element. A directive could also have element selector, like |
From @chaosmonster on September 8, 2018 9:8 I agree. I do this so many times that it feels a little bit cumbersome.
That way I can override it where ever I need to. Maybe something like this (using the prefixes defined in the angular.json) would be an option. |
From @robwormald on September 9, 2018 21:53 Thoughts: I agree with the stated problem (I forget this myself all the time!) but I'm not convinced that changing the default is a good idea. RE: cognitive overhead - my argument would be that any time that Angular diverges from the default browser behavior, that in itself increases the cognitive overhead (and load on docs team, etc). It would likely break a number of apps as well - at least if we were to change something in the internals of Angular/Renderer, and (sorry) I'd argue again there's an increase in cognitive load here, because now something "invisible" (not in your component.css file) is changing behavior (and now you have to go look at the docs! How do I override it? etc ) If we want to avoid invisible / non-local problems (that is, I generally shouldn't have to look elsewhere to understand how a component works), I think that means that defaults like this have to exist in your We could use schematics or similar to augment the default css template, so :host {
display: block;
} One nice thing here is that many new developers have no idea that the One drawback here is that you then have to keep things "in sync" - if you change those default later, they won't affect already generated components. We could provide a :host {
display: block;
} @Component({
styleUrls: [
'app/defaults.css',
'./foo.component.css'
]
}) There's an argument to be made for CSS Variables (Custom Properties) here as well, because in a modern browser, rather than duplicating the :root {
--default-component-display: block;
} Then the generated :host {
display: var(--default-component-display);
} This one feels the best to me:
This requires CSS Custom Property support - the good news is this is every browser BUT IE nowadays: https://caniuse.com/#feat=css-variables Since Angular does require a compiler pass, we could potentially make a best effort to "downlevel" those properties - at compile time, rewrite the dynamic CSS is forgiving, so if we were to generate something like this - on IE, the 2nd line doesn't parse (and so the 1st line is used) - on browsers that support CSS Variables, the :host {
display: block;
display: var(--default-component-display, block);
} This is broadly useful for theming as well, and potentially Material. |
From @SanderElias on September 10, 2018 4:27 @robwormald I would even prefer this over changing the default. I don't agree that changing the default would raise the cognitive load. But let us not go there, as we agree on the solution path. I prefer your solution because as you said, it exposes new devs to Also exposes it them to CSS custom properties. which is another plus. It is about time that this will get some more exposure and broader pick-up. I could live with the fall-back for IE, but I would like to see a setting to turn that off. Ie support should be a thing of the past. It is for me already (for a long time), but aside from that, I don't think we should carry this forward. Having a setting means that in a couple of years, we can switch the default of the setting, to not generate the IE fallback anymore(but still be available for devs that still need to support Ie). |
From @chaosmonster on September 10, 2018 6:17 I really like the idea to solve this via schematics. About resolving css variables at build time. |
I like the idea of If we want less cognitive overhead, and less magic, then not only we wouldn't add more default files, but I'd say also not to go for CSS variables for this specific case. The CSS code itself would be commented like the CLI does with polyfills, and introduced in a feature (minor version) release. Later, it can be decided whether to uncomment it by default in a major version release, along with explanation in the release announcement of course. |
@Meligy This change won't add a single file. It will add a bit of context to existing ones.
As this is in no way a breaking change, I don't think we need to wait for the next big release. As it happens on generation it only has influence on newly generated components, and exisitng ones remain untouched. |
Regardless of the other points, this would be a breaking change as existing tooling solutions could very well assume that newly generated stylesheets are empty. Something else to note is that a version of the css would be needed for each supported stylesheet preprocessor. Generate creates a stylesheet file specific to the default style setting. Also, if this is indeed a sane default for all components, would this be better addressed at the Angular level itself? |
@clydin By no breaking change, I meant it's not a breaking change for Angular or any app that is written with it. |
After discussion in the CLI meeting, I just wanted to add that Google has a web components doc which recommends setting There is also an interesting foot gun, in that if a user manually adds :host {
display: block;
}
:host([hidden]) {
display: none;
} Somewhat unrelated, but another mistake a user might run into is using |
I discussed this with @StephenFluin and @jelbourn, both of whom were on board with the idea of adding this to the CLI schematic as an option. Since we have a PR for this, I think it's reasonable to go forward with it. It's just a matter of nailing down the best way of implementing this. Also, just to include this here: @jelbourn argued against my previous point about |
When this will be availabe for the current version? I see that It already merge to angular master branch |
The PR was merged on Thursday so it just missed the last release, however it should be included in 9.1.0-next.3 which should be published later today. Considering I don't think there's any more work to do here, I think we can close this issue. Just waiting for it to roll out. |
This issue has been automatically locked due to inactivity. Read more about our automatic conversation locking policy. This action has been performed automatically by a bot. |
From @SanderElias on September 7, 2018 11:19
I'm submitting a...
Current behaviour
When you create a new component, by default it gets no setting for css display.
Expected behavior
it should have
display:block
by default.What is the motivation/use case for changing the behavior?
I know this has been discussed before (#5960), and pops up in (#12340) too.
Still, I'm putitng this back on the roll. For the following reasons:
display:block
most of the time (for me, its 99+%)I want to elaborate on point 3 a bit. The same discussion has been held in by standarts committee, If you follow along that discussion you will see that the general consensus is that it is actually a good idea, but it can't be changed because that would break the web. Angular does not have that issue. Angular can change this, and the impact of the change would be minimal. probably something that has less impact as for example rxjs 5 to 6.
Others:
If we don't do this change, at least there should be more emphasis on this in the documentation.
Copied from original issue: angular/angular#25856
The text was updated successfully, but these errors were encountered: