How can i spicify slector rule conditionally in sass? - sass

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.

Related

Function value as $color argument in Sass darken()

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?

Laravel - Is there any blade directive to check if a variable exists and is set to a specific value?

Laravel blade provides many directives, but does it have any shortcut to check if a variable exists and is set to a specific value?
Right now I'm doing something like this : -
#if(isset($request['optional_columns']['reason']) && ($request['optional_columns']['reason'] === "true")
// Do something ...
#endif
The template is looking really messy right now, I would like to use something which looks neat and clean.
You could nest your #if directive within a an #isset directive.
#isset($request['optional_columns']['reason'])
#if ($request['optional_columns']['reason'] === 'true')
// Do something
#endif
#endisset
Or you could define a custom blade directive:
Blade::if('setTrue', function($var) {
return isset($var) && $var === 'true';
});
#setTrue($request['optional_columns']['reason'])
// Do something
#endsetTrue
Answering my own question :
I solved the problem by creating a custom blade directive.
Created a custom service provider and then defined my own blade directive as follows :
use Illuminate\Support\Facades\Blade;
class BladeServiceProvider extends ServiceProvider {
public function register() {
}
public function boot() {
Blade::directive('existsandsetto', function ($expression) {
list($var, $value) = explode(',', $expression);
$var = trim($var);
$value = trim($value);
return "<?php if(isset($var) && $var === $value) : ?>";
});
Blade::directive('endexistsandsetto', function ($expression) {
return '<?php endif; ?>';
});
}
}
Usage in Blade template :
#existsandsetto($foo, 'bar')
// $foo exists and is set to 'bar'
#endexistsandsetto
Don't forget to register the custom service provider.

Laravel builder get value

How I can take event_id column value?
Thanks
Change
I'm using laravel-admin and I have to show header with event name, but when the event do not have participant get error:
Trying to get property 'event' of non-object (View: /usr/home/gernikaracing/Apps/timing/vendor/encore/laravel-admin/resources/views/grid/table.blade.php)
Code
protected function grid()
{
$grid = new Grid(new Participant);
$eventId = request()->route('event');
$grid->model()
->where('event_id', $eventId)
->orderBy('number', 'asc');
$grid->header(function ($query) {
$event = $query->first()->event->name;
return "<div style='background-color:#f7f7f7; font-size: 18px; text-align: center; padding: 7px 10px; margin-top: 0;'>$event</div>";
});
$grid->authorized(__('Authorized'))->editable()->sortable();
$grid->number(__('Number'))->sortable();
$grid->driver()->full_name(__('Driver'))->sortable();
$grid->admin_user()->name(__('Author'))->sortable();
$grid->updated_at(__('Updated at'))->sortable();
$grid->disableCreateButton();
$grid->disableFilter();
$grid->disableActions();
$grid->disableExport();
$grid->disableRowSelector();
return $grid;
}
$builder->get()->first()->event_id;

Auto generated sass maps

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;
};

Laravel validation with custom condition

I'm trying to setup validation rule with condition but have no idea how to do following:
In my form I have title_url (array for multiple language versions). I want to have unique title_url but only when module_cat_id in the form has same value as existing rows in DB.
This is my rule:
'title_url.*' => 'required|min:3|unique:modules_cat_lang,title_url'
Any ideas how to solve this?
You can define your custom similar to code below:
\Validator::extend('custom_validator', function ($attribute, $value, $parameters) {
foreach ($value as $v) {
$query = \DB::table('modules_cat_lang') // use model if you have
->where('title_url', $v)
->where('module_cat_id', \Input::get('module_cat_id'));
if ($query->exists()) {
return false;
}
}
});
'title_url.*' => 'required|min:3|custom_validator'
Read more here https://laravel.com/docs/5.3/validation#custom-validation-rules .
If you want to add your own custom validation logic to the existing laravel validator then You can use after hooks. Please have a look at the below examples.
Reference : https://laravel.com/docs/8.x/validation#adding-after-hooks-to-form-requests
Example 1(Without Parameter)
$validator->after(function ($validator) {
if ('your condition') {
$validator->errors()->add('field', 'Something went wrong!');
}
});
Example 2(With Parameter) // Here you can pass a custom parameter($input)
$validator->after(function ($validator) use ($input) {
if ($input) {
$validator->errors()->add('field', 'Something went wrong!');
}
});

Resources