scss-library/components/00_Global/_selectorHook.scss

90 lines
1.9 KiB
SCSS

$-hooks: ();
@use "sass:meta";
/*
* A system to store a context for a CSS-Selector.
* Can be used to apply speacial rules when mixins are combined
*/
@function str-replace($string, $search, $replace: '') {
$index: str-index($string, $search);
@if $index {
@return str-slice($string, 1, $index - 1)+$replace+str-replace(str-slice($string, $index + str-length($search)), $search, $replace);
}
@return $string;
}
/*
* Generates a key-safe version of the selector
*/
@function _generate_selector_identifier() {
$-path: str-replace(#{&}, '.', '');
$-path: str-replace(#{$-path}, ' ', '');
@return $-path;
}
@function hook($-key, $-value) {
$-path: _generate_selector_identifier();
$-context: ();
@if map-has-key($-hooks, $-path) {
$-context: map-get($-hooks, $-path);
}
$-context: map-merge($-context, ($-key: $-value));
$-hooks: map-merge($-hooks, ($-path: $-context)) !global;
@return 'Creating Hook for '+#{&}+' with '+#{$-key}+': '+#{$-value};
}
@function append-hook($-kwargs) {
$-path: _generate_selector_identifier();
$-hooks: map-merge($-hooks, ($-path: map-merge(get-hook(), $-kwargs))) !global;
@return 'Added '+inspect($-kwargs)+' to the hook of '+#{&};
}
@function has-hook() {
@return map-has-key($-hooks, _generate_selector_identifier());
}
@function get-hook() {
$-map: map-get($-hooks, _generate_selector_identifier());
@if $-map !=null {
@return $-map;
}
@return ('selectors': ());
}
@function get-hook-value($-key) {
$-hook: get-hook();
@if $-hook !=null {
@if map-has-key($-hook, $-key) {
@return map-get($-hook, $-key);
}
}
@return null;
}
@mixin RV-Utils__Hook--catch {
@if has-hook() {
@content;
}
}
@mixin RV-Utils__Hook--throw($-kwargs, $-css-selectors...) {
@debug append-hook($-kwargs);
}