Skip to content
This repository was archived by the owner on Feb 23, 2024. It is now read-only.

Commit b9519c6

Browse files
committed
Add a functional range bar
1 parent 748a1e9 commit b9519c6

File tree

3 files changed

+128
-6
lines changed

3 files changed

+128
-6
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
.wp-block-woocommerce-simple-price-filter {
2+
--low: 0%;
3+
--high: 100%;
4+
--range-color: currentColor;
5+
6+
.range {
7+
position: relative;
8+
margin: 15px 0;
9+
10+
.range-bar {
11+
position: relative;
12+
height: 4px;
13+
background: linear-gradient(90deg, transparent var(--low), var(--range-color) 0, var(--range-color) var(--high), transparent 0) no-repeat 0 100%/100% 100%;
14+
15+
&::before {
16+
content: '';
17+
position: absolute;
18+
top: 0;
19+
left: 0;
20+
width: 100%;
21+
height: 100%;
22+
background: currentColor;
23+
opacity: .2;
24+
}
25+
}
26+
27+
input[type="range"] {
28+
position: absolute;
29+
top: 50%;
30+
left: 0;
31+
width: 100%;
32+
height: 0;
33+
margin: 0;
34+
padding: 0;
35+
36+
&.active {
37+
z-index: 10;
38+
}
39+
}
40+
}
41+
}

assets/js/blocks/simple-price-filter/view.js

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,25 +33,53 @@ wpx( {
3333
filters: {
3434
minPrice: parseFloat( initialMinPrice ) || 0,
3535
maxPrice: parseFloat( initialMaxPrice ) || Infinity,
36+
maxRange: 90, // TODO: get this value from SSR.
37+
isMinActive: true,
38+
isMaxActive: false,
39+
},
40+
},
41+
derived: {
42+
filters: {
43+
rangeStyle: ( { state } ) => {
44+
const { minPrice, maxPrice, maxRange } = state.filters;
45+
return {
46+
'--low': `${ ( 100 * minPrice ) / maxRange }%`,
47+
'--high': `${ ( 100 * maxPrice ) / maxRange }%`,
48+
};
49+
},
3650
},
3751
},
3852
actions: {
3953
filters: {
4054
setMinPrice: ( { state, event } ) => {
4155
const value = parseFloat( event.target.value ) || 0;
4256
state.filters.minPrice = value;
43-
navigate( getHrefWithFilters( { state } ) );
4457
},
4558
setMaxPrice: ( { state, event } ) => {
46-
const value = parseFloat( event.target.value ) || Infinity;
59+
const value =
60+
parseFloat( event.target.value ) || state.filters.maxRange;
4761
state.filters.maxPrice = value;
62+
},
63+
updateProducts: ( { state } ) => {
4864
navigate( getHrefWithFilters( { state } ) );
4965
},
5066
reset: ( { state } ) => {
5167
state.filters.minPrice = 0;
5268
state.filters.maxPrice = Infinity;
5369
navigate( getHrefWithFilters( { state } ) );
5470
},
71+
updateActiveHandle: ( { state, event } ) => {
72+
const { minPrice, maxPrice, maxRange } = state.filters;
73+
const { target, offsetX } = event;
74+
const xPos = offsetX / target.offsetWidth;
75+
const minPos = minPrice / maxRange;
76+
const maxPos = maxPrice / maxRange;
77+
78+
state.filters.isMinActive =
79+
Math.abs( xPos - minPos ) < Math.abs( xPos - maxPos );
80+
81+
state.filters.isMaxActive = ! state.filters.isMinActive;
82+
},
5583
},
5684
},
5785
} );

src/BlockTypes/SimplePriceFilter.php

Lines changed: 57 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,64 @@ public function render( $attributes = [], $content = '', $block = null ) {
2222
$wrapper_attributes = get_block_wrapper_attributes();
2323
$min_price = get_query_var( self::MIN_PRICE_QUERY_VAR );
2424
$max_price = get_query_var( self::MAX_PRICE_QUERY_VAR );
25-
return "<div $wrapper_attributes>
26-
<label>Min price: <input type='text' wp-on:change='actions.filters.setMinPrice' wp-bind:value='state.filters.minPrice' value='$min_price'></label>
27-
<label>Max price: <input type='text' wp-on:change='actions.filters.setMaxPrice' wp-bind:value='state.filters.maxPrice' value='$max_price'></label>
25+
$max_range = 90; // TODO: get this value from DB.
26+
27+
// CSS variables for the range bar style.
28+
$__low = 100 * $min_price / $max_range;
29+
$__high = 100 * $max_price / $max_range;
30+
$range_style = "--low: $__low%; --high: $__high%";
31+
32+
return "
33+
<div $wrapper_attributes>
34+
<div
35+
class='range'
36+
style='$range_style'
37+
wp-bind:style='derived.filters.rangeStyle'
38+
wp-on:mousemove='actions.filters.updateActiveHandle'
39+
>
40+
<div class='range-bar'></div>
41+
<input
42+
class='active'
43+
type='range'
44+
value='$min_price'
45+
min='0'
46+
max='$max_range'
47+
wp-bind:value='state.filters.minPrice'
48+
wp-class:active='state.filters.isMinActive'
49+
wp-on:input='actions.filters.setMinPrice'
50+
wp-on:change='actions.filters.updateProducts'
51+
>
52+
<input
53+
class=''
54+
type='range'
55+
value='$max_price'
56+
min='0'
57+
max='$max_range'
58+
wp-bind:value='state.filters.maxPrice'
59+
wp-class:active='state.filters.isMaxActive'
60+
wp-on:input='actions.filters.setMaxPrice'
61+
wp-on:change='actions.filters.updateProducts'
62+
>
63+
</div>
64+
<div>
65+
<input
66+
type='text'
67+
value='$min_price'
68+
wp-bind:value='state.filters.minPrice'
69+
wp-on:input='actions.filters.setMinPrice'
70+
wp-on:change='actions.filters.updateProducts'
71+
>
72+
<input
73+
type='text'
74+
value='$max_price'
75+
wp-bind:value='state.filters.maxPrice'
76+
wp-on:input='actions.filters.setMaxPrice'
77+
wp-on:change='actions.filters.updateProducts'
78+
>
79+
</div>
2880
<button wp-on:click='actions.filters.reset'>Reset</button>
29-
</div>";
81+
</div>
82+
";
3083
}
3184

3285
/**

0 commit comments

Comments
 (0)