@elementor/editor-site-navigation 4.5.0-1210 → 4.5.0-1212
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.js +266 -106
- package/dist/index.mjs +245 -85
- package/package.json +10 -10
- package/src/components/panel/posts-list/collapsible-list.tsx +4 -1
- package/src/components/panel/posts-list/post-list-pagination-actions.tsx +76 -0
- package/src/components/panel/posts-list/posts-collapsible-list.tsx +61 -15
- package/src/hooks/use-post-list-pagination.ts +87 -0
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import * as React from 'react';
|
|
2
|
-
import { type PropsWithChildren, type ReactNode, useId } from 'react';
|
|
2
|
+
import { type PropsWithChildren, type ReactNode, type Ref, useId } from 'react';
|
|
3
3
|
import { CollapseIcon } from '@elementor/editor-ui';
|
|
4
4
|
import { Box, Chip, Collapse, Divider, List, ListItemButton, ListItemText, Stack } from '@elementor/ui';
|
|
5
5
|
|
|
@@ -11,6 +11,7 @@ import {
|
|
|
11
11
|
|
|
12
12
|
type Props = {
|
|
13
13
|
count: number;
|
|
14
|
+
headerRef?: Ref< HTMLDivElement >;
|
|
14
15
|
isOpen: boolean;
|
|
15
16
|
onOpenChange: ( isOpen: boolean ) => void;
|
|
16
17
|
title: string;
|
|
@@ -33,6 +34,7 @@ function getSectionHeaderAriaLabel( title: string, count: number ) {
|
|
|
33
34
|
|
|
34
35
|
export default function CollapsibleList( {
|
|
35
36
|
count,
|
|
37
|
+
headerRef,
|
|
36
38
|
isOpen,
|
|
37
39
|
onOpenChange,
|
|
38
40
|
title,
|
|
@@ -58,6 +60,7 @@ export default function CollapsibleList( {
|
|
|
58
60
|
} }
|
|
59
61
|
>
|
|
60
62
|
<ListItemButton
|
|
63
|
+
ref={ headerRef }
|
|
61
64
|
id={ labelId }
|
|
62
65
|
aria-controls={ contentId }
|
|
63
66
|
aria-expanded={ isOpen }
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import * as React from 'react';
|
|
2
|
+
import { CollapseIcon } from '@elementor/editor-ui';
|
|
3
|
+
import { Box, Button, CircularProgress, Stack } from '@elementor/ui';
|
|
4
|
+
import { __ } from '@wordpress/i18n';
|
|
5
|
+
|
|
6
|
+
const listPaginationButtonProps = {
|
|
7
|
+
color: 'secondary' as const,
|
|
8
|
+
size: 'small' as const,
|
|
9
|
+
variant: 'outlined' as const,
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
type PostListPaginationActionsProps = {
|
|
13
|
+
canLoadMore: boolean;
|
|
14
|
+
canShowLess: boolean;
|
|
15
|
+
isLoadingMore: boolean;
|
|
16
|
+
loadMoreButtonRef?: React.RefObject< HTMLButtonElement | null >;
|
|
17
|
+
onLoadMore: () => void;
|
|
18
|
+
onShowLess: () => void;
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
export default function PostListPaginationActions( {
|
|
22
|
+
canLoadMore,
|
|
23
|
+
canShowLess,
|
|
24
|
+
isLoadingMore,
|
|
25
|
+
loadMoreButtonRef,
|
|
26
|
+
onLoadMore,
|
|
27
|
+
onShowLess,
|
|
28
|
+
}: PostListPaginationActionsProps ) {
|
|
29
|
+
if ( ! canLoadMore && ! canShowLess ) {
|
|
30
|
+
return null;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
const loadMoreLabel = __( 'Load more', 'elementor' );
|
|
34
|
+
const showLessLabel = __( 'Show less', 'elementor' );
|
|
35
|
+
|
|
36
|
+
const loadMoreButton = (
|
|
37
|
+
<Button
|
|
38
|
+
{ ...listPaginationButtonProps }
|
|
39
|
+
ref={ loadMoreButtonRef }
|
|
40
|
+
aria-label={ loadMoreLabel }
|
|
41
|
+
disabled={ isLoadingMore }
|
|
42
|
+
endIcon={ isLoadingMore ? undefined : <CollapseIcon open={ false } /> }
|
|
43
|
+
fullWidth
|
|
44
|
+
onClick={ onLoadMore }
|
|
45
|
+
sx={ { flex: 1, my: 0.5 } }
|
|
46
|
+
>
|
|
47
|
+
{ isLoadingMore ? <CircularProgress size={ 20 } /> : loadMoreLabel }
|
|
48
|
+
</Button>
|
|
49
|
+
);
|
|
50
|
+
|
|
51
|
+
const showLessButton = (
|
|
52
|
+
<Button
|
|
53
|
+
{ ...listPaginationButtonProps }
|
|
54
|
+
aria-label={ showLessLabel }
|
|
55
|
+
endIcon={ <CollapseIcon open /> }
|
|
56
|
+
fullWidth
|
|
57
|
+
onClick={ onShowLess }
|
|
58
|
+
sx={ { flex: 1, my: 0.5 } }
|
|
59
|
+
>
|
|
60
|
+
{ showLessLabel }
|
|
61
|
+
</Button>
|
|
62
|
+
);
|
|
63
|
+
|
|
64
|
+
if ( canShowLess && canLoadMore ) {
|
|
65
|
+
return (
|
|
66
|
+
<Box sx={ { px: 2, width: '100%' } }>
|
|
67
|
+
<Stack direction="row" spacing={ 1 }>
|
|
68
|
+
{ showLessButton }
|
|
69
|
+
{ loadMoreButton }
|
|
70
|
+
</Stack>
|
|
71
|
+
</Box>
|
|
72
|
+
);
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
return <Box sx={ { px: 2, width: '100%' } }>{ canShowLess ? showLessButton : loadMoreButton }</Box>;
|
|
76
|
+
}
|
|
@@ -1,17 +1,19 @@
|
|
|
1
1
|
import * as React from 'react';
|
|
2
|
-
import { useEffect, useMemo, useState } from 'react';
|
|
3
|
-
import { Box,
|
|
2
|
+
import { useEffect, useMemo, useRef, useState } from 'react';
|
|
3
|
+
import { Box, Skeleton, Typography } from '@elementor/ui';
|
|
4
4
|
import { __ } from '@wordpress/i18n';
|
|
5
5
|
|
|
6
6
|
import { postTypesMap } from '../../../api/post';
|
|
7
7
|
import { useContentPanelCoordinator } from '../../../contexts/content-panel-coordinator';
|
|
8
8
|
import { usePostListContext } from '../../../contexts/post-list-context';
|
|
9
9
|
import { useHomepage } from '../../../hooks/use-homepage';
|
|
10
|
+
import { usePostListPagination } from '../../../hooks/use-post-list-pagination';
|
|
10
11
|
import { usePosts } from '../../../hooks/use-posts';
|
|
11
12
|
import SectionAddButton from '../section-add-button';
|
|
12
13
|
import CollapsibleList from './collapsible-list';
|
|
13
14
|
import ErrorState from './error-state';
|
|
14
15
|
import PostListItem from './post-list-item';
|
|
16
|
+
import PostListPaginationActions from './post-list-pagination-actions';
|
|
15
17
|
|
|
16
18
|
const SEARCH_DEBOUNCE_MS = 300;
|
|
17
19
|
|
|
@@ -25,6 +27,10 @@ export default function PostsCollapsibleList( { isOpenByDefault = false }: Props
|
|
|
25
27
|
const [ isOpen, setIsOpen ] = useState( isOpenByDefault );
|
|
26
28
|
const trimmedSearch = searchQuery.trim();
|
|
27
29
|
const [ debouncedSearch, setDebouncedSearch ] = useState( trimmedSearch );
|
|
30
|
+
const focusAfterShowLessRef = useRef( false );
|
|
31
|
+
const listTopRef = useRef< HTMLDivElement | null >( null );
|
|
32
|
+
const loadMoreButtonRef = useRef< HTMLButtonElement | null >( null );
|
|
33
|
+
const sectionHeaderRef = useRef< HTMLDivElement | null >( null );
|
|
28
34
|
|
|
29
35
|
useEffect( () => {
|
|
30
36
|
const handle = window.setTimeout( () => setDebouncedSearch( trimmedSearch ), SEARCH_DEBOUNCE_MS );
|
|
@@ -78,6 +84,48 @@ export default function PostsCollapsibleList( { isOpenByDefault = false }: Props
|
|
|
78
84
|
return sorted;
|
|
79
85
|
}, [ posts, isPageType, homepageId ] );
|
|
80
86
|
|
|
87
|
+
const pagination = usePostListPagination( {
|
|
88
|
+
fetchNextPage,
|
|
89
|
+
hasNextPage,
|
|
90
|
+
isFetchingNextPage,
|
|
91
|
+
loadedCount: filteredPosts.length,
|
|
92
|
+
resetKey: `${ type }:${ debouncedSearch }`,
|
|
93
|
+
totalCount: total,
|
|
94
|
+
} );
|
|
95
|
+
|
|
96
|
+
const displayedPosts = useMemo(
|
|
97
|
+
() => filteredPosts.slice( 0, pagination.visibleCount ),
|
|
98
|
+
[ filteredPosts, pagination.visibleCount ]
|
|
99
|
+
);
|
|
100
|
+
|
|
101
|
+
const handleShowLess = () => {
|
|
102
|
+
focusAfterShowLessRef.current = true;
|
|
103
|
+
pagination.handleShowLess();
|
|
104
|
+
|
|
105
|
+
const listTop = listTopRef.current;
|
|
106
|
+
|
|
107
|
+
if ( listTop && typeof listTop.scrollIntoView === 'function' ) {
|
|
108
|
+
listTop.scrollIntoView( { block: 'nearest' } );
|
|
109
|
+
}
|
|
110
|
+
};
|
|
111
|
+
|
|
112
|
+
useEffect( () => {
|
|
113
|
+
if ( ! focusAfterShowLessRef.current ) {
|
|
114
|
+
return;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
focusAfterShowLessRef.current = false;
|
|
118
|
+
|
|
119
|
+
const loadMoreButton = loadMoreButtonRef.current;
|
|
120
|
+
|
|
121
|
+
if ( loadMoreButton ) {
|
|
122
|
+
loadMoreButton.focus();
|
|
123
|
+
return;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
sectionHeaderRef.current?.focus();
|
|
127
|
+
}, [ pagination.canLoadMore, pagination.visibleCount ] );
|
|
128
|
+
|
|
81
129
|
useEffect( () => {
|
|
82
130
|
if ( debouncedSearch && filteredPosts.length > 0 ) {
|
|
83
131
|
setIsOpen( true );
|
|
@@ -108,6 +156,7 @@ export default function PostsCollapsibleList( { isOpenByDefault = false }: Props
|
|
|
108
156
|
return (
|
|
109
157
|
<CollapsibleList
|
|
110
158
|
count={ displayCount }
|
|
159
|
+
headerRef={ sectionHeaderRef }
|
|
111
160
|
isOpen={ isOpen }
|
|
112
161
|
onOpenChange={ setIsOpen }
|
|
113
162
|
title={ sectionTitle }
|
|
@@ -120,22 +169,19 @@ export default function PostsCollapsibleList( { isOpenByDefault = false }: Props
|
|
|
120
169
|
</Typography>
|
|
121
170
|
</Box>
|
|
122
171
|
) }
|
|
123
|
-
{
|
|
172
|
+
<Box ref={ listTopRef } />
|
|
173
|
+
{ displayedPosts.map( ( post ) => {
|
|
124
174
|
return <PostListItem key={ post.id } post={ post } />;
|
|
125
175
|
} ) }
|
|
126
176
|
{ [ 'duplicate', 'create' ].includes( editMode.mode ) && <PostListItem /> }
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
{ isFetchingNextPage ? <CircularProgress /> : __( 'Load more', 'elementor' ) }
|
|
136
|
-
</Button>
|
|
137
|
-
</Box>
|
|
138
|
-
) }
|
|
177
|
+
<PostListPaginationActions
|
|
178
|
+
canLoadMore={ pagination.canLoadMore }
|
|
179
|
+
canShowLess={ pagination.canShowLess }
|
|
180
|
+
isLoadingMore={ isFetchingNextPage }
|
|
181
|
+
loadMoreButtonRef={ loadMoreButtonRef }
|
|
182
|
+
onLoadMore={ pagination.handleLoadMore }
|
|
183
|
+
onShowLess={ handleShowLess }
|
|
184
|
+
/>
|
|
139
185
|
</CollapsibleList>
|
|
140
186
|
);
|
|
141
187
|
}
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
import { useCallback, useEffect, useRef, useState } from 'react';
|
|
2
|
+
|
|
3
|
+
import { POST_PER_PAGE } from '../api/post';
|
|
4
|
+
|
|
5
|
+
type UsePostListPaginationArgs = {
|
|
6
|
+
fetchNextPage: () => void;
|
|
7
|
+
hasNextPage: boolean;
|
|
8
|
+
isFetchingNextPage: boolean;
|
|
9
|
+
loadedCount: number;
|
|
10
|
+
resetKey: string;
|
|
11
|
+
totalCount: number;
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
export function usePostListPagination( {
|
|
15
|
+
fetchNextPage,
|
|
16
|
+
hasNextPage,
|
|
17
|
+
isFetchingNextPage,
|
|
18
|
+
loadedCount,
|
|
19
|
+
resetKey,
|
|
20
|
+
totalCount,
|
|
21
|
+
}: UsePostListPaginationArgs ) {
|
|
22
|
+
const [ visibleCount, setVisibleCount ] = useState( POST_PER_PAGE );
|
|
23
|
+
const pendingVisibleTargetRef = useRef< number | null >( null );
|
|
24
|
+
|
|
25
|
+
useEffect( () => {
|
|
26
|
+
setVisibleCount( POST_PER_PAGE );
|
|
27
|
+
pendingVisibleTargetRef.current = null;
|
|
28
|
+
}, [ resetKey ] );
|
|
29
|
+
|
|
30
|
+
useEffect( () => {
|
|
31
|
+
const target = pendingVisibleTargetRef.current;
|
|
32
|
+
|
|
33
|
+
if ( target === null ) {
|
|
34
|
+
return;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
if ( loadedCount >= target ) {
|
|
38
|
+
setVisibleCount( target );
|
|
39
|
+
pendingVisibleTargetRef.current = null;
|
|
40
|
+
return;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
if ( ! isFetchingNextPage ) {
|
|
44
|
+
setVisibleCount( Math.min( target, loadedCount ) );
|
|
45
|
+
pendingVisibleTargetRef.current = null;
|
|
46
|
+
}
|
|
47
|
+
}, [ isFetchingNextPage, loadedCount ] );
|
|
48
|
+
|
|
49
|
+
const canShowLess = visibleCount > POST_PER_PAGE;
|
|
50
|
+
const canLoadMore = visibleCount < loadedCount || hasNextPage;
|
|
51
|
+
|
|
52
|
+
const handleLoadMore = useCallback( () => {
|
|
53
|
+
if ( isFetchingNextPage || ! canLoadMore ) {
|
|
54
|
+
return;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
const targetVisible = Math.min( visibleCount + POST_PER_PAGE, totalCount );
|
|
58
|
+
|
|
59
|
+
if ( targetVisible <= loadedCount ) {
|
|
60
|
+
setVisibleCount( Math.min( targetVisible, loadedCount ) );
|
|
61
|
+
return;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
if ( hasNextPage ) {
|
|
65
|
+
pendingVisibleTargetRef.current = targetVisible;
|
|
66
|
+
fetchNextPage();
|
|
67
|
+
return;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
if ( visibleCount < loadedCount ) {
|
|
71
|
+
setVisibleCount( Math.min( visibleCount + POST_PER_PAGE, loadedCount ) );
|
|
72
|
+
}
|
|
73
|
+
}, [ canLoadMore, fetchNextPage, hasNextPage, isFetchingNextPage, loadedCount, totalCount, visibleCount ] );
|
|
74
|
+
|
|
75
|
+
const handleShowLess = useCallback( () => {
|
|
76
|
+
pendingVisibleTargetRef.current = null;
|
|
77
|
+
setVisibleCount( POST_PER_PAGE );
|
|
78
|
+
}, [] );
|
|
79
|
+
|
|
80
|
+
return {
|
|
81
|
+
canLoadMore,
|
|
82
|
+
canShowLess,
|
|
83
|
+
handleLoadMore,
|
|
84
|
+
handleShowLess,
|
|
85
|
+
visibleCount,
|
|
86
|
+
};
|
|
87
|
+
}
|