-
Notifications
You must be signed in to change notification settings - Fork 381
/
index.js
154 lines (134 loc) · 4.1 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
/**
* External dependencies
*/
import styled from 'styled-components';
import ResizeObserver from 'resize-observer-polyfill';
/**
* WordPress dependencies
*/
import { useLayoutEffect, useRef, useState, useCallback } from '@wordpress/element';
import { __, sprintf } from '@wordpress/i18n';
/**
* Internal dependencies
*/
import { useStory } from '../../../app';
import { LeftArrow, RightArrow, GridView } from '../../button';
const PAGE_WIDTH = 72;
const PAGE_HEIGHT = 128;
const Wrapper = styled.div`
position: relative;
display: grid;
grid: "left-navigation carousel right-navigation" auto / 53px 1fr 53px;
color: ${ ( { theme } ) => theme.colors.fg.v1 };
`;
const Area = styled.div`
grid-area: ${ ( { area } ) => area };
display: flex;
align-items: center;
justify-content: center;
flex-direction: column;
padding: 16px 0 24px;
`;
const List = styled( Area )`
flex-direction: row;
align-items: flex-start;
justify-content: ${ ( { hasHorizontalOverflow } ) => hasHorizontalOverflow ? 'flex-start' : 'center' };
overflow-x: ${ ( { hasHorizontalOverflow } ) => hasHorizontalOverflow ? 'scroll' : 'hidden' };
`;
const Page = styled.button`
padding: 0;
margin: 0 5px;
border: 3px solid ${ ( { isActive, theme } ) => isActive ? theme.colors.selection : theme.colors.bg.v1 };
height: ${ PAGE_HEIGHT }px;
width: ${ PAGE_WIDTH }px;
background-color: ${ ( { isActive, theme } ) => isActive ? theme.colors.fg.v1 : theme.colors.mg.v1 };
flex: none;
`;
const GridViewButton = styled( GridView )`
position: absolute;
bottom: 24px;
`;
function Carousel() {
const { state: { pages, currentPageIndex, currentPageId }, actions: { setCurrentPage } } = useStory();
const [ hasHorizontalOverflow, setHasHorizontalOverflow ] = useState( false );
const listRef = useRef();
const pageRefs = useRef( [] );
useLayoutEffect( () => {
const observer = new ResizeObserver( ( entries ) => {
for ( const entry of entries ) {
const offsetWidth = entry.contentBoxSize ? entry.contentBoxSize.inlineSize : entry.contentRect.width;
setHasHorizontalOverflow( Math.ceil( listRef.current.scrollWidth ) > Math.ceil( offsetWidth ) );
}
} );
observer.observe( listRef.current );
return () => observer.disconnect();
}, [ pages.length ] );
useLayoutEffect( () => {
if ( hasHorizontalOverflow ) {
const currentPageRef = pageRefs.current[ currentPageId ];
if ( ! currentPageRef || ! currentPageRef.scrollIntoView ) {
return;
}
currentPageRef.scrollIntoView( {
inline: 'center',
behavior: 'smooth',
} );
}
}, [ currentPageId, hasHorizontalOverflow, pageRefs ] );
const handleClickPage = ( page ) => () => setCurrentPage( { pageId: page.id } );
const scrollBy = useCallback( ( offset ) => {
if ( ! listRef.current.scrollBy ) {
listRef.current.scrollLeft += offset;
return;
}
listRef.current.scrollBy( {
left: offset,
behavior: 'smooth',
} );
}, [ listRef ] );
return (
<Wrapper>
<Area area="left-navigation">
<LeftArrow
isHidden={ ! hasHorizontalOverflow }
onClick={ () => scrollBy( -( 2 * PAGE_WIDTH ) ) }
width="24"
height="24"
aria-label={ __( 'Scroll Left', 'amp' ) }
/>
</Area>
<List area="carousel" ref={ listRef } hasHorizontalOverflow={ hasHorizontalOverflow }>
{ pages.map( ( page, index ) => {
const isCurrentPage = index === currentPageIndex;
return (
<Page
key={ index }
onClick={ handleClickPage( page ) }
isActive={ isCurrentPage }
ref={ ( el ) => {
pageRefs.current[ page.id ] = el;
} }
aria-label={ isCurrentPage ? sprintf( __( 'Page %s (current page)', 'amp' ), index + 1 ) : sprintf( __( 'Go to page %s', 'amp' ), index + 1 ) }
/>
);
} ) }
</List>
<Area area="right-navigation">
<RightArrow
isHidden={ ! hasHorizontalOverflow }
onClick={ () => scrollBy( ( 2 * PAGE_WIDTH ) ) }
width="24"
height="24"
aria-label={ __( 'Scroll Right', 'amp' ) }
/>
<GridViewButton
isDisabled
width="24"
height="24"
aria-label={ __( 'Grid View', 'amp' ) }
/>
</Area>
</Wrapper>
);
}
export default Carousel;