setCSSVariable method
void
setCSSVariable(
- String identifier,
- String value
)
override
Implementation
@override
void setCSSVariable(String identifier, String value) {
final List<String>? deps = _propertyDependencies[identifier];
if (DebugFlags.shouldLogCssVar(identifier, deps)) {
cssLogger.info('[var][set] id=$identifier new="$value" target=${target.tagName}');
}
if (DebugFlags.enableBackgroundLogs && identifier.startsWith('--tw-gradient-')) {
// Tailwind gradients rely heavily on custom properties. Log these at a low
// verbosity gate so we can diagnose bad tokenization (e.g., stray ';').
cssLogger.finer('[Background][var][set] id=$identifier value="$value"');
}
// Snapshot old value before mutation for transition heuristics.
// Prefer the exact prior token text, including alias var(...) if present.
String? prevRaw = _identifierStorage != null ? _identifierStorage![identifier] : null;
if (prevRaw == null && _variableStorage != null && _variableStorage![identifier] != null) {
// Preserve the alias token form, e.g., 'var(--a[, fallback])'.
prevRaw = _variableStorage![identifier]!.toString();
}
// CSS-wide keywords have special meaning for custom properties too.
// Custom properties are inherited, so:
// --x: inherit; / --x: unset;
// behaves like removing the local override and inheriting from the parent.
final String trimmed = value.trim();
final String lower = trimmed.toLowerCase();
if (lower == INHERIT || lower == 'unset') {
_identifierStorage?.remove(identifier);
_variableStorage?.remove(identifier);
if (_identifierStorage != null && _identifierStorage!.isEmpty) _identifierStorage = null;
if (_variableStorage != null && _variableStorage!.isEmpty) _variableStorage = null;
if (DebugFlags.shouldLogCssVar(identifier, deps)) {
cssLogger.info('[var][set] id=$identifier stored-as=inherit prev=${prevRaw ?? 'null'}');
}
if (_propertyDependencies.containsKey(identifier)) {
_notifyCSSVariableChanged(identifier, trimmed, prevRaw);
} else {
_clearColorCacheForVariable(identifier);
if (DebugFlags.shouldLogCssVar(identifier)) {
cssLogger.info('[var][notify] id=$identifier no-deps; cleared-color-cache-only');
}
}
return;
}
// Canonicalize CSS-wide keyword case for custom properties.
if (lower == INITIAL) {
value = INITIAL;
}
if (isSingleVarFunction(value)) {
CSSVariable? variable = CSSVariable.tryParse(this, value);
if (variable != null) {
_variableStorage ??= HashMap<String, CSSVariable>();
_variableStorage![identifier] = variable;
if (DebugFlags.shouldLogCssVar(identifier, deps)) {
cssLogger.info('[var][set] id=$identifier stored-as=alias prev=${prevRaw ?? 'null'}');
}
} else {
_identifierStorage ??= HashMap<String, String>();
_identifierStorage![identifier] = value;
if (DebugFlags.shouldLogCssVar(identifier, deps)) {
cssLogger.info('[var][set] id=$identifier stored-as=raw prev=${prevRaw ?? 'null'}');
}
}
} else {
_identifierStorage ??= HashMap<String, String>();
_identifierStorage![identifier] = value;
if (DebugFlags.shouldLogCssVar(identifier, deps)) {
cssLogger.info('[var][set] id=$identifier stored-as=raw prev=${prevRaw ?? 'null'}');
}
}
if (_propertyDependencies.containsKey(identifier)) {
if (DebugFlags.shouldLogCssVar(identifier, _propertyDependencies[identifier])) {
cssLogger.info('[var][notify] id=$identifier deps=${_propertyDependencies[identifier]?.join(',') ?? '[]'}');
}
_notifyCSSVariableChanged(identifier, value, prevRaw);
} else {
// No dependencies recorded yet (e.g., first parse may have used a cached color string).
// Clear common color cache keys so next parse recomputes with the new variable value.
_clearColorCacheForVariable(identifier);
if (DebugFlags.shouldLogCssVar(identifier)) {
cssLogger.info('[var][notify] id=$identifier no-deps; cleared-color-cache-only');
}
}
}