mirror of
https://github.com/phuoc-ng/csslayout.git
synced 2025-08-11 00:24:12 +02:00
feat: Rating
This commit is contained in:
7
contents/_includes/patterns/rating.njk
Normal file
7
contents/_includes/patterns/rating.njk
Normal file
@@ -0,0 +1,7 @@
|
||||
<div class="rating">
|
||||
<button class="rating__star">☆</button>
|
||||
<button class="rating__star">☆</button>
|
||||
<button class="rating__star">☆</button>
|
||||
<button class="rating__star">☆</button>
|
||||
<button class="rating__star">★</button>
|
||||
</div>
|
@@ -93,6 +93,7 @@ eleventyExcludeFromCollections: true
|
||||
{% pattern "Custom radio button" %}{% include "patterns/custom-radio-button.njk" %}{% endpattern %}
|
||||
{% pattern "Input addon" %}{% include "patterns/input-addon.njk" %}{% endpattern %}
|
||||
{% pattern "Radio switch" %}{% include "patterns/radio-switch.njk" %}{% endpattern %}
|
||||
{% pattern "Rating" %}{% include "patterns/rating.njk" %}{% endpattern %}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
|
63
contents/rating.md
Normal file
63
contents/rating.md
Normal file
@@ -0,0 +1,63 @@
|
||||
---
|
||||
layout: layouts/post.njk
|
||||
title: Rating
|
||||
description: Create a star rating with CSS flexbox
|
||||
keywords: css flexbox, css star rating
|
||||
---
|
||||
|
||||
## HTML
|
||||
|
||||
```html
|
||||
<div class="rating">
|
||||
<button class="rating__star">☆</button>
|
||||
<button class="rating__star">☆</button>
|
||||
<button class="rating__star">☆</button>
|
||||
<button class="rating__star">☆</button>
|
||||
<button class="rating__star">★</button>
|
||||
</div>
|
||||
```
|
||||
|
||||
## CSS
|
||||
|
||||
```css
|
||||
.rating {
|
||||
/* Center the content */
|
||||
align-items: center;
|
||||
display: flex;
|
||||
justify-content: center;
|
||||
|
||||
flex-direction: row-reverse;
|
||||
}
|
||||
|
||||
.rating__star:hover,
|
||||
.rating__star:hover ~ .rating__star {
|
||||
color: transparent;
|
||||
}
|
||||
|
||||
/*
|
||||
Make all previous stars selected when hover on a star
|
||||
Note that we use \`flex-direction: row-reverse\` on the container
|
||||
*/
|
||||
.rating__star:hover:before,
|
||||
.rating__star:hover ~ .rating__star:before {
|
||||
color: #eab308;
|
||||
content: '★';
|
||||
left: 0;
|
||||
position: absolute;
|
||||
}
|
||||
|
||||
.rating__star {
|
||||
font-size: 1.5rem;
|
||||
|
||||
/* Reset styles for button */
|
||||
background-color: transparent;
|
||||
border: transparent;
|
||||
margin: 0 2px;
|
||||
padding: 0;
|
||||
|
||||
/* Used to position the hover state */
|
||||
position: relative;
|
||||
}
|
||||
```
|
||||
|
||||
{% demo %}{% include "patterns/rating.njk" %}{% enddemo %}
|
@@ -1,97 +0,0 @@
|
||||
import * as React from 'react';
|
||||
import Head from 'next/head';
|
||||
|
||||
import { Pattern } from '../../constants/Pattern';
|
||||
import { PatternLayout } from '../../layouts/PatternLayout';
|
||||
import BrowserFrame from '../../placeholders/BrowserFrame';
|
||||
|
||||
interface StarProps {
|
||||
isActive: boolean;
|
||||
}
|
||||
|
||||
const Star: React.FC<StarProps> = ({ isActive }) => {
|
||||
return <button className="p-rating-star">{isActive ? '★' : '☆'}</button>;
|
||||
};
|
||||
|
||||
const Details: React.FC<{}> = () => {
|
||||
return (
|
||||
<PatternLayout pattern={Pattern.Rating}>
|
||||
<Head>
|
||||
<meta name="description" content="Create a star rating with CSS flexbox" />
|
||||
<meta name="og:description" content="Create a star rating with CSS flexbox" />
|
||||
<meta name="twitter:description" content="Create a star rating with CSS flexbox" />
|
||||
<meta name="keywords" content="css flexbox, css star rating" />
|
||||
</Head>
|
||||
<BrowserFrame
|
||||
html={`
|
||||
<div class="rating">
|
||||
<button class="rating__star">☆</button>
|
||||
<button class="rating__star">☆</button>
|
||||
<button class="rating__star">☆</button>
|
||||
<button class="rating__star">☆</button>
|
||||
<button class="rating__star">★</button>
|
||||
</div>
|
||||
`}
|
||||
css={`
|
||||
.rating {
|
||||
/* Center the content */
|
||||
align-items: center;
|
||||
display: flex;
|
||||
justify-content: center;
|
||||
|
||||
flex-direction: row-reverse;
|
||||
|
||||
font-size: 32px;
|
||||
}
|
||||
|
||||
.rating__star:hover,
|
||||
.rating__star:hover ~ .rating__star {
|
||||
color: transparent;
|
||||
}
|
||||
|
||||
/*
|
||||
Make all previous stars selected when hover on a star
|
||||
Note that we use \`flex-direction: row-reverse\` on the container
|
||||
*/
|
||||
.rating__star:hover:before,
|
||||
.rating__star:hover ~ .rating__star:before {
|
||||
color: #00449e;
|
||||
content: '\\2605';
|
||||
left: 0;
|
||||
position: absolute;
|
||||
}
|
||||
|
||||
.rating__star {
|
||||
/* Reset styles for button */
|
||||
background-color: transparent;
|
||||
border: transparent;
|
||||
margin: 0 2px;
|
||||
padding: 0;
|
||||
|
||||
/* Used to position the hover state */
|
||||
position: relative;
|
||||
}
|
||||
`}
|
||||
>
|
||||
<div
|
||||
style={{
|
||||
alignItems: 'center',
|
||||
display: 'flex',
|
||||
height: '100%',
|
||||
justifyContent: 'center',
|
||||
}}
|
||||
>
|
||||
<div className="p-rating">
|
||||
<Star isActive={false} />
|
||||
<Star isActive={false} />
|
||||
<Star isActive={false} />
|
||||
<Star isActive={false} />
|
||||
<Star isActive={true} />
|
||||
</div>
|
||||
</div>
|
||||
</BrowserFrame>
|
||||
</PatternLayout>
|
||||
);
|
||||
};
|
||||
|
||||
export default Details;
|
@@ -1,36 +0,0 @@
|
||||
import * as React from 'react';
|
||||
|
||||
import Frame from '../../placeholders/Frame';
|
||||
|
||||
const Cover: React.FC<{}> = () => {
|
||||
return (
|
||||
<Frame>
|
||||
<div
|
||||
style={{
|
||||
alignItems: 'center',
|
||||
display: 'flex',
|
||||
flexDirection: 'column',
|
||||
height: '100%',
|
||||
justifyContent: 'center',
|
||||
padding: '8px',
|
||||
}}
|
||||
>
|
||||
<div
|
||||
style={{
|
||||
alignItems: 'center',
|
||||
display: 'flex',
|
||||
justifyContent: 'center',
|
||||
}}
|
||||
>
|
||||
<div style={{ color: '#d1d5db', fontSize: '12px', padding: '2px' }}>★</div>
|
||||
<div style={{ color: '#d1d5db', fontSize: '12px', padding: '2px' }}>★</div>
|
||||
<div style={{ color: '#d1d5db', fontSize: '12px', padding: '2px' }}>★</div>
|
||||
<div style={{ color: '#d1d5db', fontSize: '12px', padding: '2px' }}>☆</div>
|
||||
<div style={{ color: '#d1d5db', fontSize: '12px', padding: '2px' }}>☆</div>
|
||||
</div>
|
||||
</div>
|
||||
</Frame>
|
||||
);
|
||||
};
|
||||
|
||||
export default Cover;
|
@@ -59,6 +59,7 @@
|
||||
@import './patterns/questions-and-answers';
|
||||
@import './patterns/radial-progress-bar';
|
||||
@import './patterns/radio-switch';
|
||||
@import './patterns/rating';
|
||||
@import './patterns/resizable-element';
|
||||
@import './patterns/ribbon';
|
||||
@import './patterns/separator';
|
||||
|
38
styles/patterns/_rating.scss
Normal file
38
styles/patterns/_rating.scss
Normal file
@@ -0,0 +1,38 @@
|
||||
.rating {
|
||||
/* Center the content */
|
||||
align-items: center;
|
||||
display: flex;
|
||||
justify-content: center;
|
||||
|
||||
flex-direction: row-reverse;
|
||||
}
|
||||
|
||||
.rating__star:hover,
|
||||
.rating__star:hover ~ .rating__star {
|
||||
color: transparent;
|
||||
}
|
||||
|
||||
/*
|
||||
Make all previous stars selected when hover on a star
|
||||
Note that we use \`flex-direction: row-reverse\` on the container
|
||||
*/
|
||||
.rating__star:hover:before,
|
||||
.rating__star:hover ~ .rating__star:before {
|
||||
color: #eab308;
|
||||
content: '★';
|
||||
left: 0;
|
||||
position: absolute;
|
||||
}
|
||||
|
||||
.rating__star {
|
||||
font-size: 1.5rem;
|
||||
|
||||
/* Reset styles for button */
|
||||
background-color: transparent;
|
||||
border: transparent;
|
||||
margin: 0 2px;
|
||||
padding: 0;
|
||||
|
||||
/* Used to position the hover state */
|
||||
position: relative;
|
||||
}
|
Reference in New Issue
Block a user