@elementor/editor-site-navigation 4.5.0-1209 → 4.5.0-1211

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@elementor/editor-site-navigation",
3
- "version": "4.5.0-1209",
3
+ "version": "4.5.0-1211",
4
4
  "private": false,
5
5
  "author": "Elementor Team",
6
6
  "homepage": "https://elementor.com/",
@@ -39,16 +39,16 @@
39
39
  "dev": "tsup --config=../../tsup.dev.ts"
40
40
  },
41
41
  "dependencies": {
42
- "@elementor/editor-app-bar": "4.5.0-1209",
43
- "@elementor/editor-documents": "4.5.0-1209",
44
- "@elementor/editor-panels": "4.5.0-1209",
45
- "@elementor/editor-sidebar": "4.5.0-1209",
46
- "@elementor/editor-ui": "4.5.0-1209",
47
- "@elementor/editor-v1-adapters": "4.5.0-1209",
48
- "@elementor/env": "4.5.0-1209",
42
+ "@elementor/editor-app-bar": "4.5.0-1211",
43
+ "@elementor/editor-documents": "4.5.0-1211",
44
+ "@elementor/editor-panels": "4.5.0-1211",
45
+ "@elementor/editor-sidebar": "4.5.0-1211",
46
+ "@elementor/editor-ui": "4.5.0-1211",
47
+ "@elementor/editor-v1-adapters": "4.5.0-1211",
48
+ "@elementor/env": "4.5.0-1211",
49
49
  "@elementor/icons": "~1.78.1",
50
- "@elementor/events": "4.5.0-1209",
51
- "@elementor/query": "4.5.0-1209",
50
+ "@elementor/events": "4.5.0-1211",
51
+ "@elementor/query": "4.5.0-1211",
52
52
  "@elementor/ui": "1.41.4",
53
53
  "@wordpress/api-fetch": "^6.42.0",
54
54
  "@wordpress/i18n": "^5.13.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, Button, CircularProgress, Skeleton, Typography } from '@elementor/ui';
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
- { filteredPosts.map( ( post ) => {
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
- { hasNextPage && (
128
- <Box
129
- sx={ {
130
- display: 'flex',
131
- justifyContent: 'center',
132
- } }
133
- >
134
- <Button onClick={ fetchNextPage } color="secondary">
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
  }
@@ -1,29 +1,83 @@
1
1
  import * as React from 'react';
2
- import { Box, Typography } from '@elementor/ui';
2
+ import { Box, Chip, type ChipProps, Typography } from '@elementor/ui';
3
+ import { __ } from '@wordpress/i18n';
3
4
 
4
5
  import { useReverseHtmlEntities } from '../../hooks/use-reverse-html-entities';
5
6
  import { contentPanelListRowTitleSx } from '../panel/content-panel-chrome';
6
7
 
8
+ type StatusChipConfig = {
9
+ color: ChipProps[ 'color' ];
10
+ label: string;
11
+ };
12
+
13
+ function humanizeStatusSlug( status: string ): string {
14
+ return status.replace( /[-_]+/g, ' ' ).trim();
15
+ }
16
+
17
+ const statusChipConfig: Record< string, StatusChipConfig > = {
18
+ draft: {
19
+ color: 'default',
20
+ label: __( 'Draft', 'elementor' ),
21
+ },
22
+ future: {
23
+ color: 'default',
24
+ label: __( 'Scheduled', 'elementor' ),
25
+ },
26
+ pending: {
27
+ color: 'warning',
28
+ label: __( 'Pending Review', 'elementor' ),
29
+ },
30
+ private: {
31
+ color: 'default',
32
+ label: __( 'Private', 'elementor' ),
33
+ },
34
+ };
35
+
36
+ export function getStatusChipConfig( status: string ): StatusChipConfig {
37
+ const known = statusChipConfig[ status ];
38
+
39
+ if ( known ) {
40
+ return known;
41
+ }
42
+
43
+ return {
44
+ color: 'default',
45
+ label: humanizeStatusSlug( status ),
46
+ };
47
+ }
48
+
49
+ function isKnownPanelStatus( status: string ): boolean {
50
+ return status in statusChipConfig;
51
+ }
52
+
7
53
  const PageStatus = ( { status }: { status: string } ) => {
8
54
  if ( 'publish' === status ) {
9
55
  return null;
10
56
  }
11
57
 
58
+ const { color, label } = getStatusChipConfig( status );
59
+ const isKnownStatus = isKnownPanelStatus( status );
60
+
12
61
  return (
13
- <Typography
62
+ <Chip
63
+ color={ color }
14
64
  component="span"
15
- variant="body2"
16
- color="text.secondary"
65
+ label={ label }
66
+ size="tiny"
17
67
  sx={ {
18
- ...contentPanelListRowTitleSx,
19
- textTransform: 'capitalize',
20
- fontStyle: 'italic',
21
- whiteSpace: 'nowrap',
22
- flexBasis: 'content',
68
+ flexShrink: isKnownStatus ? 0 : 1,
69
+ maxWidth: isKnownStatus ? undefined : '45%',
70
+ minWidth: 0,
71
+ ml: 0.5,
72
+ '& .MuiChip-label': {
73
+ overflow: 'hidden',
74
+ textOverflow: 'ellipsis',
75
+ whiteSpace: 'nowrap',
76
+ },
23
77
  } }
24
- >
25
- ({ status })
26
- </Typography>
78
+ title={ label }
79
+ variant="standard"
80
+ />
27
81
  );
28
82
  };
29
83
 
@@ -48,9 +102,8 @@ const PageTitle = ( { title }: { title: string } ) => {
48
102
 
49
103
  export default function PageTitleAndStatus( { title, status }: { title: string; status: string } ) {
50
104
  return (
51
- <Box display="flex">
105
+ <Box alignItems="center" display="flex" minWidth={ 0 }>
52
106
  <PageTitle title={ title } />
53
- &nbsp;
54
107
  <PageStatus status={ status } />
55
108
  </Box>
56
109
  );
@@ -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
+ }