-
Notifications
You must be signed in to change notification settings - Fork 120
/
SidePanel.tsx
1049 lines (939 loc) · 30.4 KB
/
SidePanel.tsx
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* Copyright IBM Corp. 2020, 2024
*
* This source code is licensed under the Apache-2.0 license found in the
* LICENSE file in the root directory of this source tree.
*/
// Import portions of React that are needed.
import React, {
useState,
useEffect,
useRef,
useCallback,
ReactNode,
ForwardedRef,
MutableRefObject,
} from 'react';
import { motion, AnimatePresence, useReducedMotion } from 'framer-motion';
// Other standard imports.
import PropTypes from 'prop-types';
import cx from 'classnames';
import { moderate02 } from '@carbon/motion';
import { getDevtoolsProps } from '../../global/js/utils/devtools';
import { allPropTypes } from '../../global/js/utils/props-helper';
import { pkg } from '../../settings';
import { SIDE_PANEL_SIZES } from './constants';
import { useFocus, usePreviousValue } from '../../global/js/hooks';
// Carbon and package components we use.
import { Button } from '@carbon/react';
import { Close, ArrowLeft } from '@carbon/react/icons';
import { ActionSet } from '../ActionSet';
import {
overlayVariants,
panelVariants,
actionSetVariants,
} from './motion/variants';
import pconsole from '../../global/js/utils/pconsole';
import { ButtonProps } from '@carbon/react';
const blockClass = `${pkg.prefix}--side-panel`;
const componentName = 'SidePanel';
type SidePanelBaseProps = {
/**
* Sets the action toolbar buttons
*/
actionToolbarButtons?: ButtonProps[];
/**
* The primary actions to be shown in the side panel. Each action is
* specified as an object that will render expressive Buttons. Any Button
* props can be passed in and any other fields in the object will be
* passed through to the button element as HTML attributes.
*
* See https://react.carbondesignsystem.com/?path=/docs/components-button--default#component-api
*/
actions?: ButtonProps[];
/**
* Determines if the title will animate on scroll
*/
animateTitle?: boolean;
/**
* Sets the body content of the side panel
*/
children: ReactNode;
/**
* Sets an optional className to be added to the side panel outermost element
*/
className?: string;
/**
* Sets the close button icon description
*/
closeIconDescription?: string;
/**
* Determines whether the side panel should render the condensed version (affects action buttons primarily)
*/
condensedActions?: boolean;
/**
* Sets the current step of the side panel
*/
currentStep?: number;
/**
* Unique identifier
*/
id?: string;
/**
* Determines whether the side panel should render with an overlay
*/
includeOverlay?: boolean;
/**
* Sets the label text which will display above the title text
*/
labelText?: string;
/**
* Sets the icon description for the navigation back icon button
*/
navigationBackIconDescription?: string;
/**
* Changes the current side panel page to the previous page
*/
onNavigationBack?(): void;
/**
* Specify a handler for closing the side panel.
* This handler closes the modal, e.g. changing `open` prop.
*/
onRequestClose?(): void;
/**
* Optional function called when the side panel exit animation is complete.
* This handler can be used for any state cleanup needed before the panel is removed from the DOM.
*/
onUnmount?(): void;
/**
* Determines whether the side panel should render or not
*/
open: boolean;
/**
* Determines if the side panel is on the right or left
*/
placement?: 'left' | 'right';
/**
* Prevent closing on click outside of the panel
*/
preventCloseOnClickOutside?: boolean;
/**
* This is the selector to the element that contains all of the page content that will shrink if the panel is a slide in.
* This prop is required when using the `slideIn` variant of the side panel.
*/
selectorPageContent?: string;
/**
* Specify a CSS selector that matches the DOM element that should
* be focused when the side panel opens
*/
selectorPrimaryFocus?: string;
/**
* Sets the size of the side panel
*/
size: 'xs' | 'sm' | 'md' | 'lg' | '2xl';
/**
* Determines if this panel slides in
*/
slideIn?: boolean;
/**
* **Experimental:** Provide a `Slug` component to be rendered inside the `SidePanel` component
*/
slug?: ReactNode;
/**
* Sets the subtitle text
*/
subtitle?: ReactNode;
/**
* Sets the title text
*/
title?: string;
};
type SidePanelSlideInProps =
| {
/**
* Determines if this panel slides in
*/
slideIn?: false;
/**
* This is the selector to the element that contains all of the page content that will shrink if the panel is a slide in.
* This prop is required when using the `slideIn` variant of the side panel.
*/
selectorPageContent?: string;
}
| {
/**
* Determines if this panel slides in
*/
slideIn: true;
/**
* This is the selector to the element that contains all of the page content that will shrink if the panel is a slide in.
* This prop is required when using the `slideIn` variant of the side panel. Required for slideIn panels.
*/
selectorPageContent: string;
};
type SidePanelProps = SidePanelBaseProps & SidePanelSlideInProps;
// `any` is a work around until ActionSet is migrated to TS
const MotionActionSet = motion(ActionSet);
// Default values for props
const defaults = {
animateTitle: true,
closeIconDescription: 'Close',
currentStep: 0,
navigationBackIconDescription: 'Back',
placement: 'right',
size: 'md',
};
/**
* Side panels keep users in-context of a page while performing tasks like navigating, editing, viewing details, or configuring something new.
*/
export let SidePanel = React.forwardRef(
(
{
// The component props, in alphabetical order (for consistency).
actionToolbarButtons,
actions,
animateTitle = defaults.animateTitle,
children,
className,
closeIconDescription = defaults.closeIconDescription,
condensedActions,
currentStep = defaults.currentStep,
id = blockClass,
includeOverlay,
labelText,
navigationBackIconDescription = defaults.navigationBackIconDescription,
onNavigationBack,
onRequestClose,
onUnmount,
open,
placement = defaults.placement as SidePanelProps['placement'],
preventCloseOnClickOutside,
selectorPageContent,
selectorPrimaryFocus,
size = defaults.size as SidePanelProps['size'],
slideIn,
slug,
subtitle,
title,
// Collect any other property values passed in.
...rest
}: SidePanelProps,
ref: ForwardedRef<HTMLDivElement>
) => {
const [animationComplete, setAnimationComplete] = useState(false);
const localRef = useRef<HTMLDivElement>(null);
const sidePanelRef = ref || localRef;
const overlayRef = useRef<HTMLDivElement>(null);
const innerContentRef = useRef<HTMLDivElement>(null);
const closeRef = useRef<HTMLButtonElement>(null);
const animatedScrollRef = useRef<HTMLDivElement>(null);
const headerRef = useRef<HTMLDivElement>(null);
const titleRef = useRef<HTMLDivElement>(null);
const labelTextRef = useRef<HTMLParagraphElement>(null);
const subtitleRef = useRef<HTMLParagraphElement>(null);
const previousState = usePreviousValue({ size, open });
const [scrollAnimationDistance, setScrollAnimationDistance] = useState(-1);
const [doAnimateTitle, setDoAnimateTitle] = useState(true);
const { firstElement, keyDownListener } = useFocus(sidePanelRef);
const panelRefValue = (sidePanelRef as MutableRefObject<HTMLDivElement>)
.current;
const shouldReduceMotion = useReducedMotion();
// Title animation on scroll related state
const [labelTextHeight, setLabelTextHeight] = useState<any>(0);
useEffect(() => {
if (open && !titleRef?.current) {
setDoAnimateTitle(false);
} else {
setDoAnimateTitle(animateTitle);
}
}, [animateTitle, open]);
const titleItemsStyles = useCallback(
(progress) => {
if (subtitleRef?.current) {
const subtitleEl = subtitleRef?.current;
const height = subtitleEl?.clientHeight;
const calculatedMargin = height * progress;
subtitleEl?.style?.setProperty(
'margin-top',
`${-calculatedMargin}px`
);
}
if (labelTextRef?.current) {
const calculatedMargin = labelTextHeight * progress;
labelTextRef?.current?.style?.setProperty(
'margin-top',
`${-calculatedMargin}px`
);
}
},
[labelTextHeight]
);
useEffect(() => {
if (open && animateTitle && labelTextRef?.current) {
setLabelTextHeight(Number(labelTextRef?.current?.clientHeight || null));
}
}, [animateTitle, labelTextRef, open]);
const handleScroll = useCallback(() => {
if (doAnimateTitle && innerContentRef?.current) {
const scrollTop = innerContentRef?.current?.scrollTop;
const animationProgress =
Math.min(Number(scrollTop), scrollAnimationDistance) /
scrollAnimationDistance;
panelRefValue?.style.setProperty(
`--${blockClass}--scroll-animation-progress`,
animationProgress.toString()
);
titleItemsStyles(animationProgress);
}
}, [
doAnimateTitle,
panelRefValue?.style,
scrollAnimationDistance,
titleItemsStyles,
]);
const reducedMotion =
typeof window !== 'undefined' && window?.matchMedia
? window.matchMedia('(prefers-reduced-motion: reduce)')
: { matches: true };
// scroll panel to top going between steps
useEffect(() => {
if (sidePanelRef && panelRefValue) {
const scrollableSection =
animatedScrollRef.current ?? innerContentRef.current;
if (scrollableSection) {
scrollableSection.scrollTop = 0;
}
// The size of the panel has changed while it is still opened
// so we need to scroll it to the top and reset the header
// height css custom property
if (
previousState &&
previousState['size'] !== size &&
scrollableSection
) {
scrollableSection.scrollTop = 0;
}
}
}, [currentStep, sidePanelRef, size, previousState, id, panelRefValue]);
// Add console warning if labelText is provided without a title.
// This combination is not allowed.
useEffect(() => {
if (!title && labelText) {
console.warn(
`${componentName}: The prop \`labelText\` was provided without a \`title\`. It is required to have a \`title\` when using the \`labelText\` prop.`
);
}
}, [labelText, title]);
const checkSetDoAnimateTitle = () => {
let canDoAnimateTitle = false;
if (
panelRefValue &&
open &&
animateTitle &&
animationComplete &&
titleRef?.current &&
title &&
title.length &&
!reducedMotion.matches
) {
const titleEl = titleRef.current;
const labelHeight = labelTextRef?.current?.offsetHeight ?? 0;
const subtitleHeight = subtitleRef?.current?.offsetHeight ?? 0;
// Adjusts space at bottom of titles by changing where scrolling finishes
// Styles use border to save use of get computed style
const titleVerticalBorder = actionToolbarButtons
? titleEl.offsetHeight - titleEl.clientHeight
: 0;
const scrollAnimationDistance =
labelHeight + subtitleHeight + titleVerticalBorder;
setScrollAnimationDistance(scrollAnimationDistance);
// used to calculate the header moves
panelRefValue?.style.setProperty(
`--${blockClass}--scroll-animation-distance`,
scrollAnimationDistance.toString()
);
const scrollEl = innerContentRef.current;
if (scrollEl) {
const innerComputed = window?.getComputedStyle(
innerContentRef.current as HTMLDivElement
);
const innerPaddingHeight = innerComputed
? parseFloat(innerComputed?.paddingTop) +
parseFloat(innerComputed?.paddingBottom)
: 0;
canDoAnimateTitle =
(!!labelText || !!actionToolbarButtons || !!subtitle) &&
scrollEl.scrollHeight - scrollEl.clientHeight >=
scrollAnimationDistance + innerPaddingHeight;
}
}
if (doAnimateTitle !== canDoAnimateTitle) {
// will need updating on resize
setDoAnimateTitle(canDoAnimateTitle);
}
};
useEffect(() => {
if (
!doAnimateTitle &&
(sidePanelRef as MutableRefObject<HTMLDivElement>).current
) {
panelRefValue?.style.setProperty(
`--${blockClass}--scroll-animation-progress`,
'0'
);
}
}, [
doAnimateTitle,
handleScroll,
sidePanelRef,
innerContentRef,
open,
panelRefValue?.style,
]);
// Calculate scroll distances
useEffect(() => {
if (
panelRefValue &&
open &&
animateTitle &&
animationComplete &&
titleRef?.current &&
title &&
title.length &&
!reducedMotion.matches
) {
checkSetDoAnimateTitle();
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [
open,
doAnimateTitle /* use do instead of animateTitle directly */,
animationComplete,
handleScroll,
title,
size,
reducedMotion.matches,
id,
]);
// click outside functionality if `includeOverlay` prop is set
useEffect(() => {
const handleOutsideClick = (event) => {
if (
panelRefValue &&
overlayRef.current &&
overlayRef.current.contains(event.target) &&
onRequestClose
) {
onRequestClose();
}
};
const bodyElement = document.body;
if (includeOverlay && open) {
bodyElement.style.overflow = 'hidden';
} else if (includeOverlay && !open) {
bodyElement.style.overflow = '';
}
if (includeOverlay && !preventCloseOnClickOutside) {
document.addEventListener('click', handleOutsideClick);
}
return () => {
const bodyElement = document.body;
bodyElement.style.overflow = '';
document.removeEventListener('click', handleOutsideClick);
};
}, [
includeOverlay,
onRequestClose,
open,
preventCloseOnClickOutside,
onUnmount,
sidePanelRef,
panelRefValue,
]);
// initializes the side panel to close
const onAnimationEnd = () => {
if (!open) {
onUnmount?.();
}
setAnimationComplete(true);
};
// Set the internal state `animationComplete` to true if
// prefers reduced motion is true
useEffect(() => {
if (reducedMotion.matches) {
setAnimationComplete(true);
}
}, [reducedMotion.matches]);
// initializes the side panel to open
const onAnimationStart = () => {
setAnimationComplete(false);
};
// used to reset margins of the slide in panel when closed/closing
useEffect(() => {
if (!open && slideIn) {
const pageContentElement = selectorPageContent
? (document.querySelector(selectorPageContent) as HTMLElement)
: null;
if (placement && placement === 'right' && pageContentElement) {
pageContentElement.style.marginInlineEnd = '0';
} else if (pageContentElement) {
pageContentElement.style.marginInlineStart = '0';
}
}
}, [open, placement, selectorPageContent, slideIn]);
useEffect(() => {
if (
!open &&
previousState &&
previousState['open'] &&
reducedMotion.matches
) {
onUnmount?.();
}
}, [open, onUnmount, reducedMotion.matches, previousState]);
// used to set margins of content for slide in panel version
useEffect(() => {
if (open && slideIn) {
const pageContentElement = selectorPageContent
? (document.querySelector(selectorPageContent) as HTMLElement)
: null;
if (pageContentElement) {
pageContentElement.style.inlineSize = 'auto';
} else {
pconsole.warn(
'SidePanel prop `selectorPageContent` was not provided a selector that matches any element on your page. If an element is not found, the panel will render as a slide over.'
);
}
if (placement && placement === 'right' && pageContentElement) {
pageContentElement.style.marginInlineEnd = '0';
pageContentElement.style.transition = !reducedMotion.matches
? `margin-inline-end ${moderate02}`
: '';
pageContentElement.style.marginInlineEnd = SIDE_PANEL_SIZES[size];
} else if (pageContentElement) {
pageContentElement.style.marginInlineStart = '0';
pageContentElement.style.transition = !reducedMotion.matches
? `margin-inline-start ${moderate02}`
: '';
pageContentElement.style.marginInlineStart = SIDE_PANEL_SIZES[size];
}
}
}, [
slideIn,
selectorPageContent,
placement,
size,
reducedMotion.matches,
open,
]);
useEffect(() => {
if (open) {
setTimeout(() => {
if (selectorPrimaryFocus) {
const primeFocusEl = document?.querySelector(selectorPrimaryFocus);
if (primeFocusEl) {
(primeFocusEl as HTMLElement)?.focus();
}
} else {
firstElement?.focus();
}
}, 0);
}
}, [animationComplete, firstElement, open, selectorPrimaryFocus]);
const primaryActionContainerClassNames = cx([
`${blockClass}__actions-container`,
{
[`${blockClass}__actions-container--condensed`]: condensedActions,
},
]);
const mainPanelClassNames = cx([
blockClass,
className,
`${blockClass}`,
`${blockClass}--${size}`,
{
[`${blockClass}--right-placement`]: placement === 'right',
[`${blockClass}--left-placement`]: placement === 'left',
[`${blockClass}--slide-in`]: slideIn,
[`${blockClass}--has-slug`]: slug,
[`${blockClass}--condensed-actions`]: condensedActions,
[`${blockClass}--has-overlay`]: includeOverlay,
},
]);
const renderTitle = () => (
<div
className={cx(`${blockClass}__title`, {
[`${blockClass}__title--no-label`]: !labelTextRef.current,
})}
ref={titleRef}
>
<h2
className={`${blockClass}__title-text`}
title={title}
aria-hidden={false}
>
{title}
</h2>
{doAnimateTitle && !reducedMotion.matches && (
<h2
className={`${blockClass}__collapsed-title-text`}
title={title}
aria-hidden={true}
>
{title}
</h2>
)}
</div>
);
const renderHeader = () => {
const slugCloseSize =
actions && actions.length && /l/.test(size) ? 'md' : 'sm';
let normalizedSlug;
if (slug && slug['type']?.displayName === 'Slug') {
normalizedSlug = React.cloneElement(slug as React.ReactElement<any>, {
// slug size is sm unless actions and size > md
size: slugCloseSize,
});
}
return (
<div
className={cx(`${blockClass}__header`, {
[`${blockClass}__header--on-detail-step`]: currentStep > 0,
[`${blockClass}__header--no-title-animation`]: !doAnimateTitle,
[`${blockClass}__header--reduced-motion`]: reducedMotion.matches,
[`${blockClass}__header--has-title`]: title,
})}
ref={headerRef}
>
{/* back button */}
{currentStep > 0 && (
<Button
aria-label={navigationBackIconDescription}
kind="ghost"
size={slugCloseSize}
disabled={false}
renderIcon={(props) => <ArrowLeft size={20} {...props} />}
iconDescription={navigationBackIconDescription}
className={`${blockClass}__navigation-back-button`}
onClick={onNavigationBack}
/>
)}
{/* label */}
{title && title.length && labelText && labelText.length && (
<p className={`${blockClass}__label-text`} ref={labelTextRef}>
{labelText}
</p>
)}
{/* title */}
{title && title.length && renderTitle()}
{/* slug and close */}
<div className={`${blockClass}__slug-and-close`}>
{normalizedSlug}
<Button
aria-label={closeIconDescription}
kind="ghost"
size={slugCloseSize}
renderIcon={(props) => <Close size={20} {...props} />}
iconDescription={closeIconDescription}
className={`${blockClass}__close-button`}
onClick={onRequestClose}
ref={closeRef}
/>
</div>
{/* subtitle */}
{subtitle && (
<p
className={cx(`${blockClass}__subtitle-text`, {
[`${blockClass}__subtitle-text-no-animation-no-action-toolbar`]:
!doAnimateTitle &&
(!actionToolbarButtons || !actionToolbarButtons.length),
})}
ref={subtitleRef}
>
{subtitle}
</p>
)}
{/* action toolbar */}
{actionToolbarButtons && actionToolbarButtons.length && (
<div className={`${blockClass}__action-toolbar`}>
{actionToolbarButtons.map(
({
label,
kind,
hasIconOnly = false,
icon,
renderIcon,
tooltipPosition,
tooltipAlignment,
leading,
disabled,
className,
onClick,
...rest
}) => (
<Button
{...rest}
key={label}
kind={kind || 'ghost'}
size="sm"
renderIcon={renderIcon || icon}
iconDescription={label}
{...(hasIconOnly && {
tooltipPosition: tooltipPosition || 'bottom',
tooltipAlignment: tooltipAlignment || 'start',
})}
hasIconOnly={hasIconOnly}
disabled={disabled}
className={cx([
`${blockClass}__action-toolbar-button`,
className,
{
[`${blockClass}__action-toolbar-leading-button`]:
leading,
},
])}
onClick={onClick}
>
{leading && label}
</Button>
)
)}
</div>
)}
</div>
);
};
const renderMain = () => {
return (
<div
ref={innerContentRef}
onScroll={handleScroll}
className={cx(
`${blockClass}__inner-content`,
`${blockClass}--scrolls`,
`${
!doAnimateTitle
? `${blockClass}__inner-content--no-animated-title`
: ''
}`
)}
>
{children}
</div>
);
};
return (
<AnimatePresence>
{open && (
<>
<motion.div
{...getDevtoolsProps(componentName)}
{...rest}
id={id}
className={mainPanelClassNames}
ref={sidePanelRef}
role="complementary"
aria-label={title}
onAnimationComplete={onAnimationEnd}
onAnimationStart={onAnimationStart}
variants={panelVariants}
initial="hidden"
animate="visible"
exit="exit"
custom={{ placement, shouldReduceMotion }}
onKeyDown={keyDownListener}
>
<>
{/* header */}
{renderHeader()}
{/* main */}
{renderMain()}
</>
{/* footer */}
<MotionActionSet
actions={actions ?? []}
className={primaryActionContainerClassNames}
size={size === 'xs' ? 'sm' : size}
custom={shouldReduceMotion}
variants={actionSetVariants}
/>
</motion.div>
<AnimatePresence>
{includeOverlay && (
<motion.div
variants={overlayVariants}
initial="hidden"
animate="visible"
exit="exit"
ref={overlayRef}
className={`${blockClass}__overlay`}
/>
)}
</AnimatePresence>
</>
)}
</AnimatePresence>
);
}
);
// Return a placeholder if not released and not enabled by feature flag
SidePanel = pkg.checkComponentEnabled(SidePanel, componentName);
SidePanel.propTypes = {
/**
* Sets the action toolbar buttons
*/
actionToolbarButtons: PropTypes.arrayOf(
PropTypes.shape({
label: PropTypes.string,
leading: PropTypes.bool,
icon: PropTypes.oneOfType([PropTypes.object, PropTypes.func]),
onClick: PropTypes.func,
kind: PropTypes.oneOf(['ghost', 'tertiary', 'secondary', 'primary']),
tooltipAlignment: PropTypes.oneOf(['top', 'right', 'bottom', 'left']),
tooltipPosition: PropTypes.oneOf(['start', 'center', 'end']),
})
),
/**
* The primary actions to be shown in the side panel. Each action is
* specified as an object with optional fields: 'label' to supply the button
* label, 'kind' to select the button kind (must be 'primary', 'secondary' or
* 'ghost'), 'tooltipPosition' to select where the tooltip is placed around
* the button (must be 'top', 'right', 'bottom', or 'left'), 'tooltipAlignment'
* to select how the tooltip is aligned with the button (must be 'start',
* 'center', or 'end', 'loading' to display a loading indicator, and 'onClick' to
* receive notifications when the button is clicked. Additional fields in the
* object will be passed to the Button component, and these can include
* 'disabled', 'ref', 'className', and any other Button props. Any other
* fields in the object will be passed through to the button element as HTML
* attributes.
*
* See https://react.carbondesignsystem.com/?path=/docs/components-button--default#component-api
*/
actions: allPropTypes([
/**@ts-ignore*/
ActionSet.validateActions(),
PropTypes.arrayOf(
PropTypes.shape({
...Button.propTypes,
kind: PropTypes.oneOf([
'ghost',
'danger--ghost',
'secondary',
'danger',
'primary',
]),
tooltipPosition: PropTypes.oneOf(['top', 'right', 'bottom', 'left']),
tooltipAlignment: PropTypes.oneOf(['start', 'center', 'end']),
label: PropTypes.string,
loading: PropTypes.bool,
// we duplicate this Button prop to improve the DocGen here
onClick: Button.propTypes.onClick,
})
),
]),
/**
* Determines if the title will animate on scroll
*/
animateTitle: PropTypes.bool,
/**
* Sets the body content of the side panel
*/
children: PropTypes.oneOfType([
PropTypes.arrayOf(PropTypes.node),
PropTypes.node,
]).isRequired,
/**
* Sets an optional className to be added to the side panel outermost element
*/
className: PropTypes.string,
/**
* Sets the close button icon description
*/
closeIconDescription: PropTypes.string,
/**
* Determines whether the side panel should render the condensed version (affects action buttons primarily)
*/
condensedActions: PropTypes.bool,
/**
* Sets the current step of the side panel
*/
currentStep: PropTypes.number,
/**
* Unique identifier
*/
id: PropTypes.string,
/**
* Determines whether the side panel should render with an overlay
*/
includeOverlay: PropTypes.bool,
/**
* Sets the label text which will display above the title text
*/
labelText: PropTypes.string,
/**
* Sets the icon description for the navigation back icon button
*/
navigationBackIconDescription: PropTypes.string,
/**
* Changes the current side panel page to the previous page
*/
onNavigationBack: PropTypes.func,
/**
* Specify a handler for closing the side panel.
* This handler closes the modal, e.g. changing `open` prop.
*/
onRequestClose: PropTypes.func,
/**
* Optional function called when the side panel exit animation is complete.
* This handler can be used for any state cleanup needed before the panel is removed from the DOM.
*/
onUnmount: PropTypes.func,
/**
* Determines whether the side panel should render or not
*/
open: PropTypes.bool.isRequired,
/**
* Determines if the side panel is on the right or left
*/
placement: PropTypes.oneOf(['left', 'right']),