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

Removed dynamic stylesheet and migrated to CSS vars #2854

Open
wants to merge 12 commits into
base: master
Choose a base branch
from

Conversation

lmartorella
Copy link
Contributor

@lmartorella lmartorella commented Nov 14, 2024

Description

The proposed change is to remove the dynamic CSS classes generation for rows and margins.
This has many benefits: it will speed up relayout operations, optimize performances for high row count and and fix the CSP style compatibility.

The requirement is CSS variables, that is now widely available: https://caniuse.com/css-variables

This deprecates the styleInHead properties as well.

It overrides #2852 as well.

Checklist

  • Created tests which fail without the change (if possible)
  • All tests passing (yarn test) (well, the failing test count is not increasing after the change, the main trunk has many failures)
  • Extended the README / documentation, if necessary

Copy link
Member

@adumesny adumesny left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I really like the idea of using variables to limit changes, but having 2 styles per items now (we can optimized when 1x1 items like I did) makes me wonder if this will indeed be faster for rendering.

I've wanted to do the same for columns as we only generate CSSS for 1 & 12 and many people forget to include 2-11 and wonder why things don't render. So I would like to do this for column more so actually.

this means complete removal of gs-x,y,w,h as those are only used to refer to classes anyway. We could still support READING them (for backward support), but always clear them during writting so we only have style then.

src/gridstack.scss Outdated Show resolved Hide resolved
src/gridstack.scss Outdated Show resolved Hide resolved
src/gridstack.scss Outdated Show resolved Hide resolved
src/gridstack.ts Outdated Show resolved Hide resolved
src/gridstack.ts Show resolved Hide resolved
src/gridstack.ts Show resolved Hide resolved
src/gridstack.ts Outdated Show resolved Hide resolved
src/gridstack.ts Show resolved Hide resolved
@@ -2306,7 +2260,7 @@ export class GridStack {
this.resizeToContentCheck(false, node);
if (subGrid) {
subGrid.parentGridNode = node;
if (!subGrid.opts.styleInHead) subGrid._updateStyles(true); // re-create sub-grid styles now that we've moved
subGrid._updateStyles(); // re-create sub-grid styles now that we've moved
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

might not be needed. all _updateStyles() need to be verified if needed to be called actually... sicne the external file isn't used now.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, my changes are quite conservative, I don't have the full knowledge about the side effects expected by this call (i.e. _updateContainerHeight)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fair enough. but if there was a condition styleInHead then that means it's location based and therefore entire thing doesn't apply.

src/gridstack.ts Outdated Show resolved Hide resolved
@adiessl
Copy link

adiessl commented Nov 14, 2024

I am putting this here as you mentioned it in your comment above @adumesny: we are for example using the attributes gs-x, gs-y. gs-h and gs-w to style grid cells based on their dimensions using purely CSS. Should these no longer exist, it would be much more difficult to achieve the same dynamic styling as it would involve additional code to do so.

It should be no problem, however, if they were replaced with unitless CSS custom properties like --gs-h and so on. If they are not unitless, say --gs-h would be 100px or 33%, it would not work. So please keep in mind that there might be people using these attributes and removing them might cause breaking changes. Thank you.

@adumesny
Copy link
Member

@adiessl how would unitless CSS work for you ? wouldn't all have --gs-h then and would would you tell them appart (height of 1 vs 2) ?
so you are saying you style a cell h=1 and h=2 (or width) differently ? Maybe it could be accomplished using custom class per types instead, or key off class.height value it will become ?

@adiessl
Copy link

adiessl commented Nov 15, 2024

I forked the official Fiddle and amended the CSS to show how we are using the attributes at the moment to display the grid cells' dimensions: https://jsfiddle.net/2y7w3jL0/

Unfortunately I was mistaken about being able to use unitless CSS custom properties. A CSS custom property cannot be used as content of an after element.

@adumesny
Copy link
Member

@adiessl cool example (missing 1x1 case when no attr). we should add that as a demo.

Copy link
Member

@adumesny adumesny left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do you want to do column as well to match ?

@@ -1370,41 +1370,24 @@ describe('gridstack', function() {
afterEach(function() {
document.body.removeChild(document.getElementById('gs-cont'));
});
it('should add STYLE to parent node as a default', function() {
var options = {
it('should not add STYLE to parent node', function() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just remove - obsolete code.

spec/gridstack-spec.ts Outdated Show resolved Hide resolved
src/gridstack.scss Outdated Show resolved Hide resolved
@@ -98,6 +100,19 @@ $animation_speed: .3s !default;
> .ui-resizable-sw { cursor: sw-resize; width: 20px; height: 20px;}
> .ui-resizable-w { cursor: w-resize; width: 10px; top: 15px; bottom: 15px; }

> .ui-resizable-n {
top: var(--gs-item-margin-top);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that conflicts with 95. need to figure out why I have 0 while you have '5' - either way we don't want 2 rules...
same with rest below...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This comes from the old code:

      Utils.addCSSRule(this._styles, `${prefix} > .ui-resizable-n`, `top: ${top};`);

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yep ok, so replace my 0 with your var then (just one css rule). all your resize should merge into my existing rules.

src/gridstack.scss Outdated Show resolved Hide resolved
src/gridstack.scss Outdated Show resolved Hide resolved
@@ -1317,7 +1308,7 @@ export class GridStack {
// restore any sub-grid back
if (n.subGrid?.el) {
itemContent.appendChild(n.subGrid.el);
if (!n.subGrid.opts.styleInHead) n.subGrid._updateStyles(true); // force create
n.subGrid._updateStyles();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

again if we skipped redoing styles that were global, then most likely we don't need to do anything... need to go though all calls and only call when what we upate changes...
ONLY when cellHeigt OR margin is changed, OR number of rows for grid total height (might need to tweak that)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above, since my changes are quite conservative, I don't feel that the performances are hurting so much in removing the line. Probably better to split _updateStyles and extract _updateContainerHeight from it?

src/gridstack.ts Outdated Show resolved Hide resolved
src/gridstack.ts Outdated
@@ -1635,7 +1635,12 @@ export class GridStack {
if ((!n._moving && !n._resizing) || this._placeholder === el) {
// Set inline style, refer CSS variables
el.style.top = `calc(${n.y} * var(--gs-cell-height))`;
el.style.height = `calc(${n.h} * var(--gs-cell-height))`;
if (n.h !== 1) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

n.h > 1 ? el.style.height = calc(${n.h} * var(--gs-cell-height)) : delete el.style.height; // default h=1 set globally

to match above case.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will trigger the

Extract the assignment of "el.style.height" from this expression.sonarlint(typescript:S1121)

sonar lint issue, part of the recommended TS set.
I quite agree to that, being a "delete" part of else branch as well.
A better way is probably trying to assign an empty string to height, or undefined.

@adumesny
Copy link
Member

changes in #2874 need to be cherry picked too.

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