React Table - User COntrolled Header background Color - css

I have a re-useable table component that I am styling with vanilla CSS. I have six possible colors that I would like users to be able to pick from to change the table header background color if wanted. Ideally, this color choice should be a prop on the component, but I'm not sure how to do this.
In my CSS file I have the color variables defined:
th {
--deq-primary: #0080b7;
--daq-primary: #1ab7da;
--ddw-primary: #147995;
--dwq-primary: #034a64;
--derr-primary: #05a68b;
--dwmrc-primary: #f26322;
}
th {
background-color: var(--deq-primary);
color: white;
border: 1px solid #cecece;
}
Is it possible to set a prop on my table to accept one of these values so that the header background color can be changes within the component?
Something like
<MyTable headerColor="ddw-primary" />
the the header color would change to the ddw-primary color
UPDATE
I was able to implement Hugo Elhaj-Lahsen suggestion. The variable name is being passed as a string and I updated the example he provided to account for the -- that is required in the variable name. So the final style tag in the code looks like
return <table style={{backgroundColor: `var(--${headerColor})` }}>
{// ...}
</table>
....
>
In the component I'm passing the var name as a string:
<MyTable
columns={columns}
data={data}
headerColor="my-color-variable-name"
/>

A possible React solution without libs would be to pass the CSS variable name as props, and reference it in the style of your component. Suppose we have:
<MyTable headerColor="ddw-primary" />
and your CSS variables defined in the MyTable component. Assuming you style via the style= property, you can do:
return <table style={{backgroundColor: `var(${headerColor})` }}>
{// ...}
</table>

Related

Style child element by id prop using styled-component

I'm creating AccordionSections for every element I get from my backend this way
<AccordionSection
defaultOpen={activePackage}
headingLevel="h3"
id={
activePackage
? `active-${packageObj.type}-${key.toString()}`
: `${packageObj.type}-${key.toString()}`
}
label={label}
key={`${key.toString()}-${packageObj.type}`}
>
I'm wrapping this Accordion sections in a styled Accordion component:
<Wrapper spaceStackEnd="s">{content}</Wrapper>;
I want to show the label with the green colour only for the AccordionSection that start with active in the id:
const Wrapper = styled(Accordion)`
span > {
&:first-child {
${(properties) =>
properties.id?.startsWith('active') &&
css`
color: green;
`};
}
}
`;
Span is because the generated elements are spans. Nothing is happening. am I missing something ?
You can pass a parameter active to your component. If you have children I assume that you have multiple components. So in order to render multiple components developers usually use the map() function. The second argument of the map() function is the place of the component you want to render based on the array of data you have. you can simply do this:
<YourComponent {...yourProps} active={index === 0? "green" :
"black(default color you set)"} />
and then pass that active prop to your styled-components.
if you want know more about this you can visit this link.

Updating background image in css class on imported component in vue

I want to dynamically change the background-image on a ccs class in an imported component
How can I do that?
I have installed 'vue-range-slider' and have imported RangeSlider
The range-slider is set up the following way.
<template>
<div id="slider_div" >
<range-slider
class="slider"
min="0"
max="100">
</range-slider>
</div>
</template>
<script>
import RangeSlider from 'vue-range-slider'
import 'vue-range-slider/dist/vue-range-slider.css';
export default {
name: 'susScore',
data: function() {
return {
emoji: "../assets/emoji_small.jpg",
}
},
components: {
RangeSlider
}
</script>
<style >
#slider_div{
margin-top: 95px;
margin-left: 4%;
}
.slider{
width:200px;
}
.range-slider-knob {
background-image: url("../assets/emoji_small.jpg")
}
</style>
In this case I am sending a specific image but I want to send an image dynamically using the data option, emoji, in the component.
Question
How can I dynamically update the background-image in the imported .range-slider-knob class?
I tried using CSS variables in a previous question here on SO (Dynamically add image with css variable in vue) but got the reply that that wasn't possible
You can't use vm properties in <style> tag, but you can update emoji in data to:
emoji: require("../assets/emoji_small.jpg")
... and then pass it to any template element using:
<whatever :style={backgroundImage: `url(${emoji})`} />
A fully working example: codesanbox.
I combined require() with a computed changing loaded image based on slider value.
In the RangeSlider.vue file, line 13 shows that the component includes a named slot 'knob'. Vue Slots let you pass html/vue content into the component.
You could pass in a simple div on which you can dynamically change it's background using a method like #tao suggested. Note that a name attribute is given to match the name of the slot you are trying to replace; however, the name shouldn't be required because it is the only slot within component you are using.
<range-slider
class="slider"
min="0"
max="100">
<div
name="knob"
:style={backgroundImage: url(emoji)}
/>
</range-slider>
This method also gives you more freedom in how the knob is presented and/or manipulated with JS.

dynamically change colors css

I am trying to dynamize color changing in my application built with react and CSS modules. I want to display complementary colors based on one color for each time.
To do that I defined my colors manually
style: [
{
toolbarColor:'#c80eff',
centerWidgetColor:'#0040ff',
buttomWidgetColor:'#0040ff',
rightWidgetColor:'#ffbf00',
},
{
toolbarColor:'#c80eff',
centerWidgetColor:'#fab81e',
buttomWidgetColor:'#00ff9c',
rightWidgetColor:'#12b274',
},...
But it is a long work to do and it is impossible to define all the cases.
For that, my question is, is there any equation to get complementary colors, shades, etc based on one color reference ( hex or rgb )
You can do it in two ways:
CSS
For all CSS, use variables and mixins ( for more information read this article: http://thesassway.com/intermediate/mixins-for-semi-transparent-colors ) but for that code:
$color00: #c80eff;
$color02: #0040ff;
$color03: #00ff9c;
Defining your colors will always create consistency. Then, create a mixin of the like similar to:
```
#mixin alpha-background-color($color, $background) {
$percent: alpha($color01) * 100%;
$opaque: opacify($color, 1);
$solid-color: mix($opaque, $background, $percent);
background-color: $solid-color;
background-color: $color02;
}
```
Finally, you would apply to your item:
```
.button {
#include alpha-background-color(rgba(black, 0.5), white);
}
```
Otherwise, you can do that with JS:
set your variable for the color
$color00: #c80eff;
set a trigger in the button
<button onClick="changeColor()" > Change color </button>
set the function, something on the lines of :
const changeColor = ( opacity) => {
const b = document.querySelector('.button');
let colorChange = b.style.backgroundColor;
// change opacity
b.style.opacity = `${opacity}`;
}
changeColor('set here the opacity you would want');
document.querySelector('.button').addEventListener('click',
changeColor);
Summary and suggestion:
However, any good project will have some defined color palette and styles, if you set those up in variables in CSS then you simply re use them everywhere else in the project. Otherwise it will end up being inconsistent.
Dynamic CSS Background Color
If you're using a framework like Vue and you are receiving your data from a database that contains stuff like the colors etc, you could either have specific classes but that gets tedious...
I recently found that you could pass a css variable to your html as a style property and then use that variable in your css...
Of course, this needs to be edited to change the below red to a variable of your choosing, this is just the concept.
<style>
.myDiv {
height: 200px;
width: 200px;
background: var(--backgroundColor);
}
</style>
<div class="myDiv" style="'--backgroundColor: red;'>Some block</div>
From here, you would use Javascript to either change the value of your css variable with the below
const root = document.querySelector(':root');
root.style.setProperty('--backgroundColor', 'blue');
Or in Vue logic simply dynamically change the value with something like this
<div class="myDiv" :style="'--backgroundColor: ' + backgroundVariable + ';'">
Some Block
</div>

How to dynamically change css selector property value in react js code?

I need to dynamically change the color in the react component for specific selector.
In scss (use sass) i have the following rule:
foo.bar.var * {
color: blue;
}
I want to change it in react code, to be yellow, red or something else.
I cant use style property for element, cause i need the selector to
apply for all subchilds !=)
Is there any native ways? Or should i use Radium? Or is there any similar libs for this? Maybe css-next some hove can help with this?
I have color picker, i cant write class styles for every color =(
For some answerers NOTE:
So i have selector in some scss file, that imported in some root js file with .class * {color: $somecolor} and i need change the $somecolor in that selector, during picking colors in color picker
Maybe i can somehow set selector for all nested inside style property? or there is the way how to recursively apply css style for every nested items from the style prop?
What about
class MyComponent extends React.Component {
render() {
const yellow = true // Your condition
return(
<div className={`foo bar var ${yellow && 'yellow'}`}
My item
</div>
)
}
}
.foo.bar.var {
& * {
color: blue;
}
&.yellow * {
color: yellow;
}
}
You could define a custom CSS property (CSS variables) using the style attribute of the element and assign the value to a prop, state etc.
<div className='foo bar var' style={{ "--my-color": props.color }}></div>
The custom property would work for any selector that apply to that component or children. So you could use it like that:
foo.bar.var * {
color: var(--my-color);
}
See a snippet with similar code here
this may sound stupid . but does this work ?
import myCss from './mydesign.css';
myCss.foo.bar.var = "your color"

Accessing treechildren in xul for styling

I have this code in xul:
<treechildren id = "mainTree_treechildren">
<treeitem container="true" open="true">
<treerow>
<treecell label="&tree.actions;" />
</treerow>
<treechildren id="tree_actions_treechildren" >
<treeitem container="true" open="true">
<treerow>
<treecell label="&tree.actions.warnings;" />
</treerow>
</treeitem>
</treechildren>
</treeitem>
</treechildren>
and i have problem styling it. I need to have to different text colors for treecell in mainTree_treechildren and different text color for treecell in his child treechildren with id tree_actions_treechildren.
At the moment I have this css code
#mainTree #mainTree_treechildren::-moz-tree-cell-text { color: #000000; }
#mainTree #tree_actions_treechildren::-moz-tree-cell-text { color: #FFFFFF; }
But it doesn't work. I only get black color for every element inside mainTree_treechildren including tree_actions_treechildren.
The <treechildren> elements aren't actually being displayed - they are merely the data source for the tree widget (other data sources are also possible, e.g. RDF or XML files). Consequently, their contents cannot be styled directly. If you want to style individual tree cells differently you should use the properties attribute. Something like this should work:
<treechildren id="mainTree_treechildren">
<treeitem container="true" open="true">
<treerow>
<treecell label="&tree.actions;" />
</treerow>
<treeitem container="true" open="true">
<treerow>
<treecell label="&tree.actions.warnings;" properties="warning" />
</treerow>
</treeitem>
</treeitem>
</treechildren>
And the corresponding styles:
#mainTree_treechildren::-moz-tree-cell-text { color: #000000; }
#mainTree_treechildren::-moz-tree-cell-text(warning) { color: #FF0000; }
Note that using CSS selectors like #foo #bar isn't recommended - an ID is always unique so specifying more than one in a selector will only slow things down.

Resources