|
5 | 5 | * Please see LICENSE files in the repository root for full details.
|
6 | 6 | */
|
7 | 7 |
|
8 |
| -import React, { type JSX, useEffect, useId, useState } from "react"; |
| 8 | +import React, { type JSX, useEffect, useId, useRef, useState, type RefObject } from "react"; |
9 | 9 | import { ChatFilter, IconButton } from "@vector-im/compound-web";
|
10 | 10 | import ChevronDownIcon from "@vector-im/compound-design-tokens/assets/web/icons/chevron-down";
|
11 | 11 |
|
@@ -33,47 +33,53 @@ export function RoomListPrimaryFilters({ vm }: RoomListPrimaryFiltersProps): JSX
|
33 | 33 | const { isVisible, rootRef, nodeRef } = useIsNodeVisible<HTMLLIElement, HTMLUListElement>({ threshold: 0.5 });
|
34 | 34 | const { filters, onFilterChange } = useFilters(vm.primaryFilters, isExpanded, isVisible);
|
35 | 35 |
|
| 36 | + const { ref: containerRef, isExpanded: isSafeExpanded } = useAnimateFilter<HTMLDivElement>(isExpanded); |
| 37 | + |
36 | 38 | return (
|
37 |
| - <Flex id={id} className="mx_RoomListPrimaryFilters" gap="var(--cpd-space-3x)" data-expanded={isExpanded}> |
38 |
| - <Flex |
39 |
| - as="ul" |
40 |
| - role="listbox" |
41 |
| - aria-label={_t("room_list|primary_filters")} |
42 |
| - align="center" |
43 |
| - gap="var(--cpd-space-2x)" |
44 |
| - wrap="wrap" |
45 |
| - ref={rootRef} |
46 |
| - > |
47 |
| - {filters.map((filter) => ( |
48 |
| - <li |
49 |
| - ref={filter.active ? nodeRef : undefined} |
50 |
| - role="option" |
51 |
| - aria-selected={filter.active} |
52 |
| - key={filter.name} |
| 39 | + <div className="mx_RoomListPrimaryFilters"> |
| 40 | + <div ref={containerRef} className="mx_RoomListPrimaryFilters_container" data-expanded={isSafeExpanded}> |
| 41 | + <Flex id={id} className="mx_RoomListPrimaryFilters_animated" gap="var(--cpd-space-3x)"> |
| 42 | + <Flex |
| 43 | + as="ul" |
| 44 | + role="listbox" |
| 45 | + aria-label={_t("room_list|primary_filters")} |
| 46 | + align="center" |
| 47 | + gap="var(--cpd-space-2x)" |
| 48 | + wrap="wrap" |
| 49 | + ref={rootRef} |
| 50 | + > |
| 51 | + {filters.map((filter) => ( |
| 52 | + <li |
| 53 | + ref={filter.active ? nodeRef : undefined} |
| 54 | + role="option" |
| 55 | + aria-selected={filter.active} |
| 56 | + key={filter.name} |
| 57 | + > |
| 58 | + <ChatFilter |
| 59 | + selected={filter.active} |
| 60 | + onClick={() => { |
| 61 | + onFilterChange(); |
| 62 | + filter.toggle(); |
| 63 | + }} |
| 64 | + > |
| 65 | + {filter.name} |
| 66 | + </ChatFilter> |
| 67 | + </li> |
| 68 | + ))} |
| 69 | + </Flex> |
| 70 | + <IconButton |
| 71 | + aria-expanded={isSafeExpanded} |
| 72 | + aria-controls={id} |
| 73 | + className="mx_RoomListPrimaryFilters_IconButton" |
| 74 | + aria-label={_t("room_list|room_options")} |
| 75 | + size="28px" |
| 76 | + onClick={() => setIsExpanded((_expanded) => !_expanded)} |
53 | 77 | >
|
54 |
| - <ChatFilter |
55 |
| - selected={filter.active} |
56 |
| - onClick={() => { |
57 |
| - onFilterChange(); |
58 |
| - filter.toggle(); |
59 |
| - }} |
60 |
| - > |
61 |
| - {filter.name} |
62 |
| - </ChatFilter> |
63 |
| - </li> |
64 |
| - ))} |
65 |
| - </Flex> |
66 |
| - <IconButton |
67 |
| - aria-expanded={isExpanded} |
68 |
| - aria-controls={id} |
69 |
| - className="mx_RoomListPrimaryFilters_IconButton" |
70 |
| - aria-label={_t("room_list|room_options")} |
71 |
| - size="28px" |
72 |
| - onClick={() => setIsExpanded((_expanded) => !_expanded)} |
73 |
| - > |
74 |
| - <ChevronDownIcon color="var(--cpd-color-icon-secondary)" /> |
75 |
| - </IconButton> |
76 |
| - </Flex> |
| 78 | + <ChevronDownIcon color="var(--cpd-color-icon-secondary)" /> |
| 79 | + </IconButton> |
| 80 | + </Flex> |
| 81 | + </div> |
| 82 | + </div> |
77 | 83 | );
|
78 | 84 | }
|
79 | 85 |
|
@@ -127,3 +133,37 @@ function useFilters(
|
127 | 133 | };
|
128 | 134 | return { filters: filterState.filters, onFilterChange };
|
129 | 135 | }
|
| 136 | + |
| 137 | +/** |
| 138 | + * A hook to animate the filter list when it is expanded or not. |
| 139 | + * @param areFiltersExpanded |
| 140 | + */ |
| 141 | +function useAnimateFilter<T extends HTMLElement>( |
| 142 | + areFiltersExpanded: boolean, |
| 143 | +): { ref: RefObject<T | null>; isExpanded: boolean } { |
| 144 | + const ref = useRef<T | null>(null); |
| 145 | + useEffect(() => { |
| 146 | + if (!ref.current) return; |
| 147 | + |
| 148 | + const observer = new ResizeObserver(() => { |
| 149 | + // Remove transition to avoid the animation to run when the new --row-height is not set yet |
| 150 | + // If the animation runs at this moment, the first row will jump |
| 151 | + ref.current?.style.setProperty("transition", "unset"); |
| 152 | + // For the animation to work, we need `grid-template-rows` to have the same unit at the beginning and the end |
| 153 | + // If px is used at the beginning, we need to use px at the end. |
| 154 | + // In our case, we use fr unit to fully grow when expanded (1fr) so we need to compute the value in fr when the filters are not expanded |
| 155 | + ref.current?.style.setProperty("--row-height", `${30 / ref?.current.scrollHeight}fr`); |
| 156 | + }); |
| 157 | + observer.observe(ref.current); |
| 158 | + return () => observer.disconnect(); |
| 159 | + }, [ref]); |
| 160 | + |
| 161 | + // We put back the transition to the element when the filters are expanded |
| 162 | + const [isExpanded, setExpanded] = useState(areFiltersExpanded); |
| 163 | + useEffect(() => { |
| 164 | + ref.current?.style.setProperty("transition", "0.1s ease-in-out"); |
| 165 | + setExpanded(areFiltersExpanded); |
| 166 | + }, [areFiltersExpanded, ref]); |
| 167 | + |
| 168 | + return { ref, isExpanded }; |
| 169 | +} |
0 commit comments