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

feat: Expose xmodule xblocks Sass variables as vanilla CSS variables #35233

Merged
merged 2 commits into from
Aug 20, 2024

Conversation

farhan
Copy link
Contributor

@farhan farhan commented Aug 6, 2024

Ticket: #35173

Following script is used to extract out the required sass variables to expose:**

# Search for all Sass var references, sort them, and save them to a file using ripgrep
rg '\$[a-zA-Z0-9-_]*'   xmodule/assets/**/*scss --no-line-number --no-filename --only-matching \
    | sort | uniq > variable_references.txt

# Search for local declarations of Sass vars (like '$selected_color: ...'), sort them,
# and save them to a file.
rg '\$[a-zA-Z0-9-_]*: ' xmodule/assets/**/*scss --no-line-number --no-filename --only-matching \
    | sed 's/..$//' | sort | uniq > local_variable_declarations.txt

# Compare the set of var references with the set of local declarations.
# Variables which are referenced but *don't* have local declarations must be themeable Sass variable.
# Save them to a file.
diff --unified local_variable_declarations.txt variable_references.txt \
    | rg '^\+' | sed 's/^.//'  > themable_variables.txt

How to test:

Test in xmodule xblocks:

  1. Checkout the PR branch
  2. Run npm run compile-sass or npm run compile-sass-dev in lms shell. Restart lms if changes don't reflect.
  3. Open any XBlock in the lms or cms. Open dev tools window and find the css file in the scripts window. Select the xblock first with the Inspect selector arrow.
  4. We can also hit following command in console to verify the variable
getComputedStyle(document.documentElement).getPropertyValue('--baseline')

Test in extracted xblocks:

  1. Checkout the extracted word-cloud xblocks PR. Follow instructions on the PR.
  2. Convert the word-cloud xblock hard code variable into css global variable in file as shared below
.input-cloud {
    margin: var(--baseline)/4
    /*margin: 5px;*/
}
  1. Install the xblock into the lms and cms. Restart both services.
  2. Now Open the installed xblock in the lms or cms and find out the value changed in the second step.
  3. We can also hit following command in console to verify the variable
getComputedStyle(document.documentElement).getPropertyValue('--baseline')

@farhan farhan force-pushed the farhan/deprecate-word-cloud-xblock branch from efdd4df to d8ff25c Compare August 6, 2024 13:08
@kdmccormick
Copy link
Member

Looking great so far @farhan . Does this capture every Sass variable that used within xmodule/assets?

Also, I just edited the issue to include a Step 4:

  1. For each builtin block....
    • Modify the block's Sass code to use the CSS variables instead of the Sass variables.
    • Tip: Start with one block and merge the PR. That way, if it breaks on edX or in Tutor, we know sooner than later.

Could you choose a builtin block, and apply Step 4 for that block in this PR?

There will also be "Step 5: Convert each blocks' Sass into CSS", but I need to do some discovery before I can provide details for that. Let's worry about Step 5 in a future PR.

@farhan
Copy link
Contributor Author

farhan commented Aug 7, 2024

@kdmccormick

Does this capture every Sass variable that used within xmodule/assets?

No, it doesn't cover all the sass variables, we are working on it. Variables like Annotators xblock's need to be discussed with @irtazaakram

There will also be "Step 5: Convert each blocks' Sass into CSS", but I need to do some discovery before I can provide details for that. Let's worry about Step 5 in a future PR.

I have converted the word-cloud xblock sass variable into css global variable as below looking into the file and it's working fine

.input-cloud {
    margin: var(--baseline)/4
    /*margin: 5px;*/
}

@farhan
Copy link
Contributor Author

farhan commented Aug 7, 2024

@kdmccormick
We have added all the sass variables now. update file

It includes all the sass variables directly import from the imports excluding the one locally defined in the files.
cc: @irtazaakram

@kdmccormick
Copy link
Member

Great. Let me know when you are ready for review.

@farhan farhan force-pushed the farhan/sass-to-css branch 2 times, most recently from 2419afa to 890155d Compare August 8, 2024 08:23
@farhan farhan changed the title feat: Converts builtin block Sass into vanilla CSS feat: Expose xmodule xblocks Sass variables as vanilla CSS variables Aug 8, 2024
@farhan farhan force-pushed the farhan/sass-to-css branch from 890155d to 45cf850 Compare August 8, 2024 08:27
@farhan farhan linked an issue Aug 8, 2024 that may be closed by this pull request
@farhan farhan changed the base branch from farhan/deprecate-word-cloud-xblock to master August 8, 2024 09:33
@farhan farhan force-pushed the farhan/sass-to-css branch from 45cf850 to 2ac1a69 Compare August 8, 2024 09:35
@farhan farhan marked this pull request as ready for review August 8, 2024 09:36
@farhan farhan force-pushed the farhan/sass-to-css branch from 2ac1a69 to 6996950 Compare August 8, 2024 09:37
@farhan farhan force-pushed the farhan/sass-to-css branch from 6996950 to 6e5e8aa Compare August 8, 2024 10:39
@kdmccormick
Copy link
Member

I believe these Sass variables are missing from _builtin-block-variables.scss:

$all-text-inputs
$base-font-size
$base-line-height
$black-t2
$bp-screen-lg
$btn-brand-focus-background
$correct
$danger
$font-bold
$general-color-accent
$gray-300
$gray-d1
$highlight
$incorrect
$medium-font-size
$partially-correct
$small-font-size
$submitted
$success
$tmg-s2
$uxpl-gray-background
$uxpl-gray-base
$uxpl-gray-dark
$warning
$warning-color
$warning-color-accent
$yellow

Here is the bash script I used to determine that (it requires ripgrep, also known as rg):

# Search for all Sass var references, sort them, and save them to a file.
rg '\$[a-zA-Z0-9-_]*'   xmodule/assets/**/*scss --no-line-number --no-filename --only-matching \
    | sort | uniq > variable_references.txt

# Search for local declarations of Sass vars (like '$selected_color: ...'), sort them,
# and save them to a file.
rg '\$[a-zA-Z0-9-_]*: ' xmodule/assets/**/*scss --no-line-number --no-filename --only-matching \
    | sed 's/..$//' | sort | uniq > local_variable_declarations.txt

# Compare the set of var references with the set of local declarations.
# Variables which are referenced but *don't* have local declarations must be themeable Sass variable.
# Save them to a file.
diff --unified local_variable_declarations.txt variable_references.txt \
    | rg '^\+' | sed 's/^.//'  > themable_variables.txt

# Search the new _builtin-block-variables.scss file to get a list of CSS->Sass mapped variables.
# Sort and save them to a file.
rg '\$[A-Za-z0-9-_]*' common/static/sass/_builtin-block-variables.scss --no-line-number --no-filename --only-matching \
    | sort | uniq > css_mapped_variables.txt

# Finally, compare the CSS->Sass-mapped variables with the themed ones we saved earlier.
# This is the list of variables that we are missing CSS->Sass mappings for.
diff --unified css_mapped_variables.txt themable_variables.txt \
    | rg '^\+' | sed 's/^.//'

I think it would be best to get all these variables into _builtin-block-variables.scss now so that we don't have bugs later. Let me know if you are curious how any part of that bash script works-- I find bash really helpful when I have to do these sort of tasks and I'd be happy to share the knowledge.

Copy link
Member

@kdmccormick kdmccormick left a comment

Choose a reason for hiding this comment

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

(see my comment above)

@farhan
Copy link
Contributor Author

farhan commented Aug 12, 2024

@kdmccormick Thanks for sharing ripgrep scripts, it's really helpful and I learned about a very handy library to deal with these kinds of stuff.

FYI: I have removed $highlight as its a local variable used in loop so couldn't come under $*: regex

PR is available for review again

@farhan farhan requested a review from kdmccormick August 12, 2024 09:32
Copy link
Member

@kdmccormick kdmccormick left a comment

Choose a reason for hiding this comment

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

Looks great, thanks @farhan !

Can you explain how you manually tested this to ensure that the CSS variables are available in legacy views?

@farhan
Copy link
Contributor Author

farhan commented Aug 16, 2024

Can you explain how you manually tested this to ensure that the CSS variables are available in legacy views?

@kdmccormick I have added my testing steps in the description of the PR. Hope this is what you are asking for.
I didn't get what is meant by legacy views

@farhan farhan requested a review from kdmccormick August 16, 2024 10:21
Copy link
Member

@kdmccormick kdmccormick left a comment

Choose a reason for hiding this comment

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

Forgive me @farhan , when I said "legacy views" what I should have said is "server-rendered views" (as opposed to MFE pages). XBlocks fall into that category, so your testing steps look perfect 👍🏻 🚀

@kdmccormick
Copy link
Member

When you squash & merge, please include a link to the ticket in the commit message.

@farhan farhan merged commit 375b9d9 into master Aug 20, 2024
49 checks passed
@farhan farhan deleted the farhan/sass-to-css branch August 20, 2024 15:05
@edx-pipeline-bot
Copy link
Contributor

2U Release Notice: This PR has been deployed to the edX staging environment in preparation for a release to production.

@edx-pipeline-bot
Copy link
Contributor

2U Release Notice: This PR has been deployed to the edX production environment.

@e0d
Copy link
Contributor

e0d commented Aug 20, 2024

Late the party, but can't this | sort | uniq just be | sort -u?

@kdmccormick
Copy link
Member

TIL 😄

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.

Expose builtin block Sass variables as CSS variables
4 participants