I'm trying to generate an automated system to define colors in SASS.
I have a map of colors named $brand-colors, and I would like the colors of this map to be used to generate the tints and shades in a second map, no matter how many colors there are in $ brand-colors.
This is the point where I arrived:
$brand-colors: (
brand-color: (
primary-color: #0096d6,
secondary-color: #1a4760,
),
) !default;
#function generate-map($map) {
#each $item, $colors in $map {
#each $color-name, $value in $colors {
#return(
$color-name: (
light-30: mix(white, $value, 30%),
light-20: mix(white, $value, 20%),
light-10: mix(white, $value, 10%),
base: $value,
dark-10: mix(black, $value, 10%),
dark-20: mix(black, $value, 20%),
dark-30: mix(black, $value, 30%),
),
);
};
};
};
$brand-palette: (
brand-palette:(
generate-map($_new-brand-colors)
),
) !default;
With the above code, I get this result from the terminal:
brand-palette:(
primary-color:(
light-30: #4db6e2,
light-20: #33abde,
light-10: #1aa1da,
base: #0096d6,
dark-10: #0087c1,
dark-20: #0078ab,
dark-30: #006996
)
)
In short, only the first key-value pair is taken, and I can not understand why.
Can someone give me an answer?
Functions and return statements
The #return statement tells the function to stop everything and return the value.
#function myFunction() {
#each $item in [a, b, c, d] {
#return $item;
}
}
myFunction() will only return a. Even though we have a loop with four items, it will run only once (the first time) because it immediately hits the #return statement.
Best practice to return once at end of function
One best practice with functions is to have a $result variable, and only call #return $result once at the end of the function
#function myFunctionFixed() {
$result;
#each $item in [a, b, c, d] {
$result: $result + a;
}
#return $result;
}
myFunctionFixed() will return abcd because we allow the loop to run from beginning to end without interupting it with #return, and we only return at the end of the function after the loop has completed.
Using map-merge to incrementally build a map
Applying the best practice described above, we can move the #return statement to the end of your function and use map-merge to incrementally build a $result-map variable.
#function generate-map($map) {
$result-map: () !default;
#each $item, $colors in $map {
#each $color-name, $value in $colors {
$result-map: map-merge($result-map,
($color-name: (
light-30: mix(white, $value, 30%),
light-20: mix(white, $value, 20%),
light-10: mix(white, $value, 10%),
base: $value,
dark-10: mix(black, $value, 10%),
dark-20: mix(black, $value, 20%),
dark-30: mix(black, $value, 30%),
))
);
};
};
#return $result-map;
};
Related
I'm trying to the the following piece of code:
public function remember(string $key, \Closure $callback, int $ttl = null): mixed
{
return \Cache::remember($key, $ttl, $callback);
}
with:
public function testRememberWhenTimeoutIsSet(): void
{
\Cache::shouldReceive('remember')
->once()
->with(\Mockery::on(function ($key, $ttl, $callback) {
return $key === 'key' && $ttl === 123 && is_callable($callback);
}))
->andReturn('any-thing');
$this->assertEquals(
'any-thing',
$this->service->remember('key', function () {}, 123)
);
}
But it keeps giving me the following error:
Mockery\Exception\NoMatchingExpectationException : No matching handler
found for Mockery_2_Illuminate_Cache_CacheManager::remember('key',
123, object(Closure)). Either the method was unexpected or its
arguments matched no expected argument list for this method
Objects: ( array ( 'Closure' => array (
'class' => 'Closure',
'identity' => '#3e713b47f2e6096de32a4437ee0381ff',
'properties' =>
array (
), ), ))
The error goes away if to completely remove with block. Any ideas?
You can consider the code below to work well:
Cache::shouldReceive('remember')
->once()
->with('key', 123, \Closure::class)
->andReturn('any-thing')
;
with method in your case doesn't need a CLOSURE matcher but only a list of expected arguments.
I'm new to scss, I want to implement theming using scss, I wrote mixins and function that returns a color value if parent class. i.e "theme-light" or "theme-dark" exits accordingly. How can I write a selector that returns a default value if the parent class doesn't exist?
#mixin themed() {
#each $theme, $map in $themes {
.theme-#{$theme} & {
$theme-map: () !global;
#each $key, $submap in $map {
$value: map-get(map-get($themes, $theme), '#{$key}');
$theme-map: map-merge($theme-map, ($key: $value)) !global;
}
#content;
$theme-map: null !global;
}
}
}
#function t($key) {
#return map-get($theme-map, $key);
}
Use case: actually, I want to implement theming in my react app. based upon the selected theme either dark/light mode the whole theme for the app would be changed.
I use Laravel 7, and I want to return an error if my Model has a relationship or not.
This error doesn't rely on a field, so I want to add a rule but without field.
I tried :
$rules [] = [
'address' => function ($attribute, $value, $fail) use ($parking) {
if (! $parking or ! $parking->parkingAddress()->count()) {
$fail("You must fill address before !");
}
}
];
Or :
$rules [] = function ($attribute, $value, $fail) use ($parking) {
if (! $parking or ! $parking->parkingAddress()->count()) {
$fail("You must fill address before !");
}
};
But it doesn't throw the error. How I can return the error ?
I'm trying to use a color value specified in a map as the $color argument for darken(), like so:
border: 1px solid darken(color(lowlight), 10);
This relies on a couple of simple functions and maps I've written (extraneous values removed):
#function gray($color) {
#return map-get($grays, $color);
}
#function color($color) {
#return map-get($colors, $color);
}
$grays: (
x-light: #f8f8f8
);
$colors: (
lowlight: gray(x-light)
);
I'm getting this error:
Argument $color of darken($color, $amount) must be a color
It works if I supply it with a variable, but not with a function value. Is there a solution to this?
Newbie here. Can anyone help me about getting the parameter in my custom validation.
Here is my validation rule :
['materials.*.receive_quantity' => 'lessthan:materials.*.quantity']
Here is my custom validation :
Validator::extend('lessthan', function ($attribute, $value, $parameters, $validator) { return $value <= $parameters[0]; });
When i dd($parameters) it return a string 'materials.*.quantity'. TIA.
First, you'll need a 5.4.18 release.
Then, try this code:
Validator::extendDependent('lessthan', function ($attribute, $value, $parameters, $validator) {
return $value <= array_get($validator->getData(), $parameters[0]);
});
And you may look at this pull request for more explanations and examples: PR#18564
P.S. And may be this is a typo in your code: your rule name is "lessthan", but you use the "<=" operator instead of "<".