Skip to content

Commit

Permalink
fix: missing css variables in chrome dev-tool (#1967)
Browse files Browse the repository at this point in the history
## Proposed change

Do not remove css variable name from metadata extracted as it is used in
the devtools
Do not fail on third party css scripts
Do not create variable for css var details
  • Loading branch information
cpaulve-1A authored Jul 16, 2024
2 parents 0ef7bcc + d38cd03 commit c110874
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 8 deletions.
1 change: 0 additions & 1 deletion packages/@o3r/styling/builders/style-extractor/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,6 @@ export default createBuilder(createBuilderWithMetricsIfInstalled<StyleExtractorB
cssVarList
.forEach((item) => {
acc.variables[item.name] = item;
delete (acc.variables[item.name] as any).name;
});
return acc;
}, previousMetadata);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@

@each $key, $value in $meta-theme {
$new-key: if($root-name != '', '#{$root-name}-#{$key}', $key);
$new-value: _meta-theme-to-otter($value, $new-key);
// We do not extract the metadata as (variable, value) pair
$new-value: if($key != details, _meta-theme-to-otter($value, $new-key), $value);
$ret: map.merge($ret, ($key: $new-value));
}

Expand Down
4 changes: 4 additions & 0 deletions packages/@o3r/styling/scss/theming/otter-theme/_material.scss
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,10 @@
@return $meta-theme;
}

@if (map.has-key($meta-theme, value) and map.has-key($meta-theme, details)) {
@return _meta-theme-to-material(map.get($meta-theme, value), $root-name, $enable-css-var);
}

@else {
$ret: $meta-theme;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,23 @@ const isStylingMessage = (message: any): message is AvailableStylingMessageConte

const getCSSRulesAppliedOnRoot = () => Array.from(document.styleSheets)
.reverse()
.reduce((acc: CSSStyleRule[], styleSheet) => acc.concat(
Array.from(styleSheet.cssRules || styleSheet.rules)
.reverse()
.filter((rule): rule is CSSStyleRule => rule instanceof CSSStyleRule && /\b:root\b/.test(rule.selectorText))
), []);
.reduce((acc: CSSStyleRule[], styleSheet) => {
let rules;
try {
rules = styleSheet.cssRules || styleSheet.rules;
} catch (err) {
console.debug(`Could not access to stylesheet ${styleSheet.href}. This might be due to network issues, please check:
- network connectivity
- CORS setup
- granted access to the stylesheet`, err);
}

return acc.concat(
Array.from(rules || [])
.reverse()
.filter((rule): rule is CSSStyleRule => rule instanceof CSSStyleRule && /\b:root\b/.test(rule.selectorText))
);
}, []);

const getCSSVariableValueInCSSStyleDeclaration = (variableName: string, style: CSSStyleDeclaration) =>
style.getPropertyValue(variableName).trim();
Expand Down Expand Up @@ -65,9 +77,10 @@ export class StylingDevtoolsMessageService implements OnDestroy {

private async sendMetadata() {
const metadata = await this.stylingDevTools.getStylingMetadata(this.options.stylingMetadataPath);
const cssRules = getCSSRulesAppliedOnRoot();
const variables = Object.values(metadata.variables).map((variable) => ({
...variable,
runtimeValue: getCSSVariableValue(`--${variable.name}`, getCSSRulesAppliedOnRoot())
runtimeValue: getCSSVariableValue(`--${variable.name}`, cssRules)
}));
this.sendMessage('getStylingVariable', { variables });
}
Expand Down

0 comments on commit c110874

Please sign in to comment.