Skip to content

Commit a045d10

Browse files
authored
fix: add location property to confetti (#18)
1 parent 2cc7011 commit a045d10

2 files changed

Lines changed: 37 additions & 4 deletions

File tree

src/action-types/class-action-type-confetti.php

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,22 @@ public function initialize() {
2727

2828
$this->keywords = [];
2929

30-
$this->properties = [];
30+
$this->properties = [ 'location' => [
31+
'name' => __( 'Location', 'interactions' ),
32+
'type' => 'select',
33+
'options' => [
34+
[ 'label' => __( 'Center', 'interactions' ), 'value' => 'center' ],
35+
[ 'label' => __( 'Top', 'interactions' ), 'value' => 'top' ],
36+
[ 'label' => __( 'Right', 'interactions' ), 'value' => 'right' ],
37+
[ 'label' => __( 'Bottom', 'interactions' ), 'value' => 'bottom' ],
38+
[ 'label' => __( 'Left', 'interactions' ), 'value' => 'left' ],
39+
[ 'label' => __( 'Top Left', 'interactions' ), 'value' => 'top-left' ],
40+
[ 'label' => __( 'Top Right', 'interactions' ), 'value' => 'top-right' ],
41+
[ 'label' => __( 'Bottom Left', 'interactions' ), 'value' => 'bottom-left' ],
42+
[ 'label' => __( 'Bottom Right', 'interactions' ), 'value' => 'bottom-right' ],
43+
],
44+
'default' => 'center',
45+
] ];
3146

3247
$this->targets = [
3348
[ 'value' => 'trigger' ],

src/action-types/frontend/confetti.js

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,24 @@
11
/**
22
* This is the frontend script loaded in the frontend if the action is used.
33
*/
4+
5+
const LOCATION_MAP = {
6+
center: { x: 0.5, y: 0.60 },
7+
top: { x: 0.5, y: 0.10 },
8+
bottom: { x: 0.5, y: 0.90 },
9+
left: { x: 0.10, y: 0.5 },
10+
right: { x: 0.90, y: 0.5 },
11+
'top-left': { x: 0.10, y: 0.10 },
12+
'top-right': { x: 0.90, y: 0.10 },
13+
'bottom-left': { x: 0.10, y: 0.90 },
14+
'bottom-right': { x: 0.90, y: 0.90 },
15+
}
16+
417
InteractRunner.addActionConfig( {
518
confetti: {
619
initAction: action => {
720
action.initActionFunction( () => {
8-
const origin = { x: 0.5, y: 0.65 }
21+
const location = action.getValue( 'location' ) || 'center'
922

1023
const targets = action.getTargets()
1124
let targetEl = targets.length ? targets[ 0 ] : window
@@ -15,11 +28,16 @@ InteractRunner.addActionConfig( {
1528
targetEl = window
1629
}
1730

31+
let origin = { x: 0.5, y: 0.60 }
32+
const pos = LOCATION_MAP[ location ] || LOCATION_MAP.center
33+
1834
if ( targetEl !== window && targetEl !== document ) {
1935
// Get the percentage of the center of the target element to the screen width
2036
const rect = targetEl.getBoundingClientRect()
21-
origin.x = ( rect.left + ( rect.width / 2 ) ) / window.innerWidth
22-
origin.y = ( rect.top + ( rect.height / 2 ) + 20 ) / window.innerHeight
37+
origin.x = ( rect.left + ( rect.width * pos.x ) ) / window.innerWidth
38+
origin.y = ( rect.top + ( rect.height * pos.y ) ) / window.innerHeight
39+
} else {
40+
origin = pos
2341
}
2442

2543
window.interactions.confetti( origin )

0 commit comments

Comments
 (0)