-
-
Notifications
You must be signed in to change notification settings - Fork 11
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
A way to easily use hierarchical accoutrement maps in Herman #345
Comments
It turns out, this function was easier to write than I thought it would be. Here's what I came up with: @use 'accoutrement' as a;
/// @group util
/// Resolves all the values in $map starting with $root.
/// Needed to turn accoutrement maps into something that Herman can display
/// (Herman can only display flat maps with simple values.)
@function flatten-accoutrement-map($map, $root: '') {
$enumMap: null;
@if ($root == '') {
$enumMap: $map;
}
@else {
$enumMap: a.get-token($map, $root)
}
@if (type-of($enumMap) != 'map') {
@return ( $root: $enumMap );
}
@else {
$result: ();
@each $key, $value in $enumMap {
$flattened-key: '#{$root}->#{$key}';
@if ($root == '') {
$flattened-key: $key;
}
@if (type-of($value) == 'map') {
$result: map.merge($result, flatten-accoutrement-map($map, $flattened-key));
}
@else {
$result: map.merge($result, ($flattened-key: $value));
}
}
@return $result;
}
} And it can be used like this to display the contents of hierarchical accoutrement maps in Herman: @use 'herman';
/// @group colors
/// @colors site-colors
@include herman.herman-add('colors', 'site-colors', flatten-accoutrement-map(site-colors.$colors));
/// Site sizes for screens below desktop
/// @group site-sizes
/// @sizes site-sm-sizes {ruler}
@include herman.herman-add('sizes', 'site-sm-sizes', flatten-accoutrement-map(a.$sizes, 'sm'));
/// Site sizes for desktop+
/// @group site-sizes
/// @sizes site-lg-sizes {ruler}
@include herman.herman-add('sizes', 'site-lg-sizes', flatten-accoutrement-map(a.$sizes, 'lg')); Herman does already display multiple map keys with the same value together (yay!). I'll leave this here in case it's useful to others. It would be awesome IMO if this were added to accoutrement or herman. |
Hi @johncrim - sorry I missed this. Accoutrement does have a map-flattening function, but it's currently private: |
That would be great. I don't have other use-cases (that I can think of right now) for flattening maps, but I'm sure they exist. My main point though, is that since accoutrement supports hierarchical maps, and the hierarchical nature is super useful, it would be nice if they worked in Herman. |
I agree. I'm also working on a spec for Sass to provide better native nested-map support. That would simplify things in both places. Should Herman support go beyond flattening maps, though? Do we want ways to render nested data? I'd be open to proposals (and then PRs) along those lines. |
I certainly agree that native nested map support would be ideal. Flattening it to view it in a table is a workaround. Extending Herman is outside my comfort zone for the time being. (I'm still trying to set aside some time to implement a couple Accoutrement issues that I raised.) |
Before implementation, we'd need to determine how we access & render nested maps - what's the API / syntax for that. Open to ideas. |
Given an accoutrement map like the following:
If I add these colors to the herman map, using:
The values in the root of the map are displayed correctly, but the submaps, and references, aren't. This problem isn't specific to displaying colors in herman - it would also be great to resolve and display hierarchical sizes, fonts, etc in herman.
If there were an accoutrement function that "flattened" the above map to:
And then the color swatch rendering combined all keys with the same value under a single swatch (it may already do this), accoutrement + herman would be a nicer combination IMO.
The workaround is to manually map the hierarchical values to a new map that's added only to herman.
It would also be great to be able to flatten sub-maps for display in herman. Eg if I have:
It would be nice to be able to display only the values within a specific submap (eg
#sm
) within herman.The text was updated successfully, but these errors were encountered: