-
Notifications
You must be signed in to change notification settings - Fork 44
/
Copy pathindex.js
172 lines (149 loc) · 4.25 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
/* eslint-disable */
// @ts-nocheck
import { useSelect, useDispatch } from '@wordpress/data';
import { useEffect, useState, useRef } from '@wordpress/element';
import { createBlock } from '@wordpress/blocks';
import { useInnerBlocksProps } from '@wordpress/block-editor';
import deprecated from '@wordpress/deprecated';
/** @jsx jsx */
import { jsx, css } from '@emotion/react';
import { ChevronLeft, ChevronRight } from './icons';
export const InnerBlockSlider = ({
parentBlockId,
slidesPerPage = 1,
allowedBlock,
template = null,
slideHeight = null,
}) => {
const [currentPage, setCurrentPage] = useState(1);
deprecated('InnerBlockSlider', {
since: '1.15.12',
version: '1.16',
alternative:
'the useInnerBlocksProps hook to render the inner blocks and then use the same JS library that powers the slider on the frontend in the editor',
plugin: '10up Block Components',
});
let innerBlockTemplate = template;
if (!innerBlockTemplate) {
innerBlockTemplate = [[allowedBlock]];
}
const slideBlocks = useSelect(
(select) => select('core/block-editor').getBlock(parentBlockId).innerBlocks,
);
const { insertBlock } = useDispatch('core/editor');
const slides = useRef();
const slideCount = useRef();
const totalPages = Math.ceil(slideBlocks.length / slidesPerPage);
const totalWidth = (100 / slidesPerPage) * slideBlocks.length;
const slideSlotWidth = 100 / slideBlocks.length;
const moveOffset = slideSlotWidth * (currentPage - 1) * slidesPerPage;
const addSlide = () => {
const created = createBlock(allowedBlock);
insertBlock(created, undefined, parentBlockId);
};
/**
* Reset page to 1 if slidesPerPage changes
*/
useEffect(() => {
setCurrentPage(1);
}, [slidesPerPage]);
/**
* If a slide is added, switch to the new slide. If one is deleted, make sure we don't
* show a non-existent slide.
*/
useEffect(() => {
if (!slideCount.current) {
slideCount.current = slideBlocks.length;
} else if (slideBlocks.length > slideCount.current) {
// Slide added
slideCount.current = slideBlocks.length;
setCurrentPage(totalPages);
} else if (slideBlocks.length < slideCount.current) {
// Slide deleted
slideCount.current = slideBlocks.length;
if (currentPage > totalPages) {
setCurrentPage(totalPages);
}
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [slideBlocks.length]);
const slidesCSS = css`
/* stylelint-disable */
width: ${totalWidth}%;
transform: translate3d(-${moveOffset}%, 0px, 0px);
${slideHeight ? `height: ${slideHeight};` : ''}
display: flex;
flex-wrap: nowrap;
& > .wp-block {
width: ${slideSlotWidth}%;
}
`;
const innerBlocksProps = useInnerBlocksProps(
{ className: 'slides', ref: slides },
{
template: innerBlockTemplate,
orientation: 'horizontal',
allowedBlocks: [allowedBlock],
},
);
const prevEnabled = currentPage > 1;
const nextEnabled = currentPage < totalPages;
return (
<div className="inner-block-slider">
<div className="slides-outer" style={{ overflow: 'hidden' }}>
<div {...innerBlocksProps} css={slidesCSS} />
</div>
<div className="navigation">
{[...Array(totalPages).keys()].map((i) => (
<button
aria-label={`Slide ${i + 1}`}
onClick={() => {
setCurrentPage(i + 1);
}}
type="button"
key={i + 1}
className={`dot ${currentPage === i + 1 ? 'current' : ''}`}
/>
))}
<button
aria-label="Add new slide"
onClick={() => {
addSlide();
}}
type="button"
className="add"
>
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" role="img">
<path d="M18 11.2h-5.2V6h-1.6v5.2H6v1.6h5.2V18h1.6v-5.2H18z" />
</svg>
</button>
</div>
<div className="controls">
<div className={`prev-container ${!prevEnabled ? 'disable' : ''}`}>
<button
onClick={() => {
if (prevEnabled) {
setCurrentPage(currentPage - 1);
}
}}
type="button"
>
<ChevronLeft />
</button>
</div>
<div className={`next-container ${!nextEnabled ? 'disable' : ''}`}>
<button
onClick={() => {
if (nextEnabled) {
setCurrentPage(currentPage + 1);
}
}}
type="button"
>
<ChevronRight />
</button>
</div>
</div>
</div>
);
};