Home > front end >  CSS in JS is great, but how to convenient handle CSS pseudo-classes
CSS in JS is great, but how to convenient handle CSS pseudo-classes

Time:09-16

CSS in JS is great, but how to convenient processing CSS pseudo-classes
Do not know the nuggets to do not support the GIF
CSS in JS is great, but how to convenient processing pseudo-classes (Pseudo - classes)? The react - the dom - the pseudo provides a similar components react - motion way, convenient for the react - dom object to provide similar CSS pseudo-classes.

We first use NPM install:

$NPM install, save the react - the dom - the pseudo
Copy the code
APIs
The react - the dom - the pseudo supports the following pseudo class:

Props to simulate pseudo class shows the default value must be
The merge of whether to use style and other state style merging true no
Disable whether to cancel the event listeners false no
Style of the default style undefined no
LinkStyle: before the link is not click style undefined no
VisitedStyle: visited click style of undefined no
FocusStyle: focus input types, such as the style of the element when an onFocus undefined no
HoverStyle: when hover the mouse moved to the style of the display whether undefined
ActiveStyle: active mouse or touch screen click on the style of the undefined no
DisableStyle when cancel event listeners style undefined no
AlwayStyle will and all styles and cover repeat the style properties of undefined no
They will according to the trigger event, and style combined return, such as {... Style,... ActiveStyle}, only existing style will merge

The combination of the style rule: {... Style,... LinkStyle,... EventStyle,... DisableStyle,... AlwayStyle}

Use the
Import the Pseudo from 'react - the dom - the Pseudo';

Export the default ()=& gt; {
Return (

Example: & lt;/div>
Style={sheet. Input}
HoverStyle={sheet. InputHover}
FocusStyle={sheet. InputFocus}

{events=& gt;

);
};

//CSS in js
Const sheet={
Input: {
FontSize: '14 px,
Border: '1 px solid rgba (0,0,0,0)',
Background: '# f3f3f3',
//to enable the transition animation
The transition: 'all 0.2 s ease - out',
},
InputHover: {
Background: '# f0f0f0',
},
InputFocus: {
Border: '1 px solid rgba (0,0,0,0.1)',
Background: '# f0f0f3',
TransitionTimingFunction: 'ease - in',
},
};
Copy the code
Which do?
The Pseudo renderProps contains the following events

OnClick: used to simulate: link and: visited pseudo-classes
An onFocus \ the Blur: used to simulate: focus pseudo-classes
OnMouseEnter \ Leave: used to simulate the: hover pseudo class
OnMouseDown \ Up: used to simulate: active pseudo-classes
If the project execution, on the mobile end would bring onm ouse? Events related to replace the onTouch? Compatible with mobile end

RenderProps way than I directly define an Input component have what advantage?
Let's see if we define an Input component, directly to simulate the: hover pseudo class

//the following code written in the markdown directly, no after run, is only used to view
The class Input the extend ponent {
React.ComState={
Hover: false
}
HandleMouseEnter=()=& gt; {
Enclosing setState ({hover: true});
}
HandleMouseLeave=()=& gt; {
Enclosing setState ({hover: false});
}
Render () {
Return & lt; Input style={this. State. Hover? {... This. Props. Style,... This. Props. HoverStyle} : this props. Style} onm ouseEnter={this. HandleMouseEnter} onm ouseLeave={this. HandleMouseLeave}/& gt;
}
}
Copy the code
Then we used in the project:

Copy the code
Everything looks good, but it is not conducive to extend, for example: if we need to give a div or SignButton also add more function, we need to write more than one component

Of course, we can also use the HOC way, write a withHover components, even so, also need to create a new component before use:

Const SignButton=withHover (SignButton);
Copy the code
In contrast, there is no RenderProps way grace:

{events=& gt;
{events=& gt;

{events=& gt;
Copy the code
The react - the dom - the pseudo can also be simpler?
Because if the child is a div or an array of objects, using childrenFuncion significance is not big, so can take shorthand:

//can be abbreviated as shown in the following, the Pseudo components is a div

//by the same token, the multiple child elements, also can such

Multiple child elements & lt;/p>

The parent is equivalent to a div


Copy the code
How do I get hover, active state, such as do in addition to the style of other events?
RenderProps parameters and the second is the inside of the Pseudo state, we can get it after doing other events, is shown in the following example, according to the hover state we modify the innerText div,

The state has four object {hover, focus, active, visited}

{(events, state)=& gt; {
Const mouseState=state. Hover? 'mouseIn' : 'mouseOut';
Return & lt; Div {... Events} & gt; {mouseState} & lt;/div>;
}}

Copy the code
How to temporary shielding event listeners?
The Pseudo to disable Settings as true

Copy the code
How to add state of all styles on the outer style?
Pseudo a alwayStyle properties, eventually return style is such a {... Style,... OtherStyle,... AlwayStyle}

Copy the code
The CSS in JS to use?
Vary from person to person, I feel better than write CSS and sass, its reason is the following:

Many animation library, such as the react - motion, the react - such as spring, object will need operation style, the style may be exist in the CSS and js, respectively, at this point, there is no CSS in js concise;
CSS in JS allows components related to code in a file is closed, we modify a component, do not need to switch back and forth files;
If the project is larger, we need segmentation module, CSS in JS to be better than the traditional CSS file segmentation with components.
How to solve some CSS JS write up in trouble?

Sass color mixing, and other functions, can be used to mix - color libraries such as the easy solution;
Sass custom variables, in the CSS in JS can easily define a globalStyles objects to achieve;
nullnullnullnullnull
  • Related