Skip to content

Commit 639806b

Browse files
committed
add half star support for ratings
1 parent 283a2d6 commit 639806b

File tree

9 files changed

+164
-61
lines changed

9 files changed

+164
-61
lines changed

_includes/featuredbox.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
<h2 class="card-title">
2121
<a class="text-dark" href="{{ post.url | absolute_url }}">{{ post.title }}</a>
2222
{% if post.rating %}
23-
<div class="mb-2 mt-1 font-weight-normal">
23+
<div class="mb-2 mt-2 font-weight-normal">
2424
{% include star_rating_postbox.html %}
2525
</div>
2626
{% endif %}

_includes/postbox.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
<h2 class="card-title">
1313
<a class="text-dark" href="{{ post.url | absolute_url }}">{{ post.title }}</a>
1414
{% if post.rating %}
15-
<div class="mb-2 mt-1 font-weight-normal">
15+
<div class="mb-2 mt-2 font-weight-normal">
1616
{% include star_rating_postbox.html %}
1717
</div>
1818
{% endif %}

_includes/star_rating.html

Lines changed: 9 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,9 @@
1-
{% if page.rating == 5 %}
2-
<span class="star-rating text-warning"> &#9733;&#9733;&#9733;&#9733;&#9733;</span>
3-
{% endif %}
4-
5-
{% if page.rating == 4 %}
6-
<span class="star-rating text-warning"> &#9733;&#9733;&#9733;&#9733;&#9734;</span>
7-
{% endif %}
8-
9-
{% if page.rating == 3 %}
10-
<span class="star-rating text-warning"> &#9733;&#9733;&#9733;&#9734;&#9734;</span>
11-
{% endif %}
12-
13-
{% if page.rating == 2 %}
14-
<span class="star-rating text-warning"> &#9733;&#9733;&#9734;&#9734;&#9734;</span>
15-
{% endif %}
16-
17-
{% if page.rating == 1 %}
18-
<span class="star-rating text-warning"> &#9733;&#9734;&#9734;&#9734;&#9734;</span>
19-
{% endif %}
1+
<div class="rating-holder">
2+
<div class="c-rating c-rating--regular" data-rating-value="{{page.rating}}">
3+
<button>1</button>
4+
<button>2</button>
5+
<button>3</button>
6+
<button>4</button>
7+
<button>5</button>
8+
</div>
9+
</div>

_includes/star_rating_postbox.html

Lines changed: 9 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,9 @@
1-
{% if post.rating == 5 %}
2-
<span class="star-rating text-warning"> &#9733;&#9733;&#9733;&#9733;&#9733;</span>
3-
{% endif %}
4-
5-
{% if post.rating == 4 %}
6-
<span class="star-rating text-warning"> &#9733;&#9733;&#9733;&#9733;&#9734;</span>
7-
{% endif %}
8-
9-
{% if post.rating == 3 %}
10-
<span class="star-rating text-warning"> &#9733;&#9733;&#9733;&#9734;&#9734;</span>
11-
{% endif %}
12-
13-
{% if post.rating == 2 %}
14-
<span class="star-rating text-warning"> &#9733;&#9733;&#9734;&#9734;&#9734;</span>
15-
{% endif %}
16-
17-
{% if post.rating == 1 %}
18-
<span class="star-rating text-warning"> &#9733;&#9734;&#9734;&#9734;&#9734;</span>
19-
{% endif %}
1+
<div class="rating-holder">
2+
<div class="c-rating c-rating--regular" data-rating-value="{{post.rating}}">
3+
<button>1</button>
4+
<button>2</button>
5+
<button>3</button>
6+
<button>4</button>
7+
<button>5</button>
8+
</div>
9+
</div>

_layouts/post.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,8 @@ <h1 class="posttitle">{{ page.title }}</h1>
5151

5252
<!-- Rating -->
5353
{% if page.rating %}
54-
<div class="rating mb-4">
55-
<strong>Rating</strong>: {% include star_rating.html %}
54+
<div class="rating mb-4 d-flex align-items-center">
55+
<strong class="mr-1">Rating:</strong> {% include star_rating.html %}
5656
</div>
5757
{% endif %}
5858

_posts/2018-01-11-customer-service.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,14 @@ image: assets/images/11.jpg
77
description: "My review of Inception movie. Acting, plot and something else in this short description."
88
featured: true
99
hidden: true
10-
rating: 5
10+
rating: 4.5
1111
---
1212

1313
Review products, books, movies, restaurant and anything you like on your Jekyll blog with Mediumish! JSON-LD ready for review property.
1414

1515
#### How to use?
1616

17-
It's actually really simple! Add the rating in your YAML front matter:
17+
It's actually really simple! Add the rating in your YAML front matter. It also supports halfs:
1818

1919
```html
2020
---
@@ -24,6 +24,6 @@ author: john
2424
categories: [ Jekyll, tutorial ]
2525
image: assets/images/11.jpg
2626
description: "My review of Inception movie. Actors, directing and more."
27-
rating: 5
27+
rating: 4.5
2828
---
2929
```

_sass/_stars.scss

Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
/// Credits to https://codepen.io/damianmuti/pen/KmEMdR
2+
3+
///
4+
$rating-min-value: 0 !default;
5+
///
6+
$rating-max-value: 5 !default;
7+
///
8+
$rating-steps-value: .25 !default;
9+
///
10+
$rating-color-empty: '%23ddd' !default;
11+
///
12+
$rating-color-full: gold !default;
13+
///
14+
$rating-color-hover: grey !default;
15+
/// This variable is a map containing the "d" attribute of each of the SVG icons
16+
$rating-icons-paths: (
17+
quarter: 'M196.208 415.2v-224.8l-139.504 20.272 100.944 98.384-23.84 138.928z',
18+
half: 'M258.672 64l-62.384 126.4-139.504 20.272 100.944 98.384-23.84 138.928 124.768-65.6v-318.4z',
19+
three-quarters: 'M321.616 190.496l-0.656-0.096-62.384-126.4-62.384 126.4-139.504 20.272 100.944 98.384-23.84 138.928 124.768-65.6 63.024 33.136z',
20+
full: 'M457.888 210.672l-139.504-20.272-62.384-126.4-62.384 126.4-139.504 20.272 100.944 98.384-23.84 138.928 124.768-65.6 124.768 65.6-23.84-138.928c0 0 100.944-98.384 100.944-98.384z'
21+
);
22+
23+
/// Creates a dynamic list of values that increment each .25 from 0 to 5
24+
/// @return {list}
25+
@function rating-values() {
26+
$rating-values: ();
27+
28+
@for $i from $rating-min-value through ($rating-max-value / $rating-steps-value) {
29+
$rating-value: abs($i * $rating-steps-value);
30+
$rating-values: append($rating-values, $rating-value, 'comma');
31+
}
32+
33+
@return $rating-values;
34+
}
35+
36+
/// Generates an SVG with a given fill color depending on the type of icon passed as parameter. The SVG string is scaped for cross-browser support.
37+
/// @param {string} $icon - Type of icon. Accepted values: `quarter`, `half`, `three-quarters` or `full`.
38+
/// @param {color} $color - Passes the fill color of the SVG icon that is being generated. Note: Hexa color values must be escaped for cross-browser support.
39+
/// @return {string} background value.
40+
@function get-icon($icon, $color) {
41+
@if not index(quarter half three-quarters full, $icon) {
42+
@error "Interaction type must be either `quarter`, `half`, `three-quarters` or `full`.";
43+
}
44+
45+
@return url('data:image/svg+xml;utf8,%3Csvg%20version%3D%221.1%22%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%20xmlns%3Axlink%3D%22http%3A%2F%2Fwww.w3.org%2F1999%2Fxlink%22%20width%3D%22512%22%20height%3D%22512%22%20viewBox%3D%220%200%20512%20512%22%3E%3Cpath%20fill%3D%22' + $color + '%22%20d%3D%22' + map-get($rating-icons-paths, $icon) + '%22%3E%3C%2Fpath%3E%3C%2Fsvg%3E') center / cover no-repeat;
46+
}
47+
48+
/// This is a CSS-only star rating component that shows the proper rating depending on a data-attribute value. JS logic behind this component should round up values per quarter.
49+
/// @param {number} $star-size [20px] - Provides width and height for each of the stars.
50+
/// @param {string} $rating-element [button] - Element to use for each of the stars.
51+
/// @param {string} $interaction-type [representative] - Indicates whether this component should be clickable or representative. Accepted values: `clickable` or `representative`.
52+
/// @example scss
53+
/// .class {
54+
/// c-rating(toem(20px), button, clickable);
55+
/// }
56+
/// @example markup
57+
/// <div class="c-rating" data-rating-value="3.25">
58+
/// <span>1</span>
59+
/// <span>2</span>
60+
/// <span>3</span>
61+
/// <span>4</span>
62+
/// <span>5</span>
63+
/// </div>
64+
@mixin c-rating($star-size: 20px, $star-element: button, $interaction-type: representative) {
65+
@if not index(clickable representative, $interaction-type) {
66+
@error "Interaction type must be either `clickable` or `representative`.";
67+
}
68+
69+
#{$star-element} {
70+
display: inline-block;
71+
float: left;
72+
width: $star-size;
73+
height: $star-size;
74+
border: 0;
75+
text-indent: -9999px;
76+
outline: none;
77+
background: get-icon(full, $rating-color-empty);
78+
79+
}
80+
81+
@each $rating-value in rating-values() {
82+
// Get the next higher integer.
83+
$rating-value-ceil: ceil($rating-value);
84+
85+
&[data-rating-value="#{$rating-value}"] {
86+
#{$star-element}:nth-child(-n+#{$rating-value-ceil}) {
87+
background: get-icon(full, $rating-color-full);
88+
}
89+
90+
#{$star-element}:nth-child(#{$rating-value-ceil}) {
91+
// Evaluate which fraction of a star this value is and add the proper background
92+
@if str-slice("#{$rating-value}", 2, 4) == '.25' {
93+
background: get-icon(quarter, $rating-color-full), get-icon(full, $rating-color-empty)
94+
}
95+
@else if str-slice("#{$rating-value}", 2, 4) == '.5' {
96+
background: get-icon(half, $rating-color-full), get-icon(full, $rating-color-empty)
97+
}
98+
@else if str-slice("#{$rating-value}", 2, 4) == '.75' {
99+
background: get-icon(three-quarters, $rating-color-full), get-icon(full, $rating-color-empty)
100+
}
101+
}
102+
}
103+
}
104+
}
105+
106+
/// Convert to EMs function
107+
/// @param {number} target - The value to be converted
108+
/// @param {number} context [$msuxf-font-size] The base font size
109+
/// @return {em} value
110+
@function toem($target, $context: 16px) {
111+
@if $target == 0 {
112+
@return 0
113+
}
114+
115+
@return $target / $context+0em;
116+
}
117+
118+
119+
.rating-holder {
120+
font-size: 16px;
121+
display: inline-block;
122+
background-color: #fff;
123+
border-radius: toem(25px);
124+
box-sizing: border-box;
125+
}
126+
127+
.c-rating {
128+
@include c-rating(toem(20px), button, clickable);
129+
130+
&--small {
131+
font-size: 50%
132+
}
133+
134+
&--big {
135+
font-size: 150%;
136+
}
137+
}

assets/css/main.scss

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,6 @@
1010

1111
// Import partials from `sass_dir` (defaults to `_sass`)
1212
@import
13-
"syntax"
13+
"syntax",
14+
"stars"
1415
;

assets/css/screen.css

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -664,19 +664,4 @@ License: https://www.wowthemes.net/freebies-license/
664664
}
665665
.sticky-top-80 {
666666
top:80px;
667-
}
668-
.star-rating {
669-
font-size:20px;
670-
}
671-
672-
a.reveal {
673-
font-size: 65%;
674-
padding: 4px;
675-
border-radius:2px;
676-
line-height:1;
677-
background: #333;
678-
color: #fff !important;
679-
font-style: italic;
680-
cursor: pointer;
681-
display:inline-block;
682667
}

0 commit comments

Comments
 (0)