Skip to content

Commit e30adf4

Browse files
authored
Fix date separator trigger ref forwarding for jump-to-date menu (#33102)
* Fix date separator trigger ref forwarding for jump-to-date menu * Normal forwarded ref is sufficient for the menu-button setup in Compound * Better comment
1 parent a210d3c commit e30adf4

2 files changed

Lines changed: 19 additions & 14 deletions

File tree

packages/shared-components/src/room/timeline/DateSeparatorView/DateSeparatorButton.tsx

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
* Please see LICENSE files in the repository root for full details.
66
*/
77

8-
import React from "react";
8+
import React, { forwardRef } from "react";
99
import { Heading, Tooltip } from "@vector-im/compound-web";
1010
import ChevronDownIcon from "@vector-im/compound-design-tokens/assets/web/icons/chevron-down";
1111

@@ -20,8 +20,6 @@ export interface DateSeparatorButtonProps {
2020
tooltipOpen?: boolean;
2121
/** Extra CSS classes to apply to the component. */
2222
className?: string;
23-
/** Optional ref for the button container element. */
24-
buttonRef?: React.Ref<HTMLDivElement>;
2523
/** Called when the pointer enters the button trigger. */
2624
onMouseEnter?: React.MouseEventHandler<HTMLDivElement>;
2725
/** Called when the pointer leaves the button trigger. */
@@ -32,19 +30,17 @@ export interface DateSeparatorButtonProps {
3230
onBlur?: React.FocusEventHandler<HTMLDivElement>;
3331
}
3432

35-
/** Interactive date separator button that opens the jump-to-date menu. */
36-
export function DateSeparatorButton({
37-
label,
38-
tooltipOpen,
39-
className,
40-
buttonRef,
41-
...props
42-
}: DateSeparatorButtonProps): React.ReactNode {
33+
/** Interactive date separator button that forwards its ref to the underlying trigger element used by Compound Menu. */
34+
export const DateSeparatorButton = forwardRef<HTMLDivElement, DateSeparatorButtonProps>(function DateSeparatorButton(
35+
{ label, tooltipOpen, className, ...props },
36+
forwardedRef,
37+
): React.ReactNode {
4338
const { translate: _t } = useI18n();
39+
4440
return (
4541
<Tooltip description={_t("room|jump_to_date")} placement="right" open={tooltipOpen}>
4642
<Flex
47-
ref={buttonRef}
43+
ref={forwardedRef}
4844
data-testid="jump-to-date-separator-button"
4945
className={className}
5046
aria-live="off"
@@ -60,4 +56,4 @@ export function DateSeparatorButton({
6056
</Flex>
6157
</Tooltip>
6258
);
63-
}
59+
});

packages/shared-components/src/room/timeline/DateSeparatorView/DateSeparatorView.test.tsx

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,13 @@
77

88
import { render, screen } from "@test-utils";
99
import { composeStories } from "@storybook/react-vite";
10-
import React from "react";
10+
import React, { createRef } from "react";
1111
import { describe, it, expect } from "vitest";
1212
import { waitFor } from "@testing-library/react";
1313
import userEvent from "@testing-library/user-event";
1414

1515
import { BaseViewModel } from "../../../core/viewmodel/BaseViewModel";
16+
import { DateSeparatorButton } from "./DateSeparatorButton";
1617
import { DateSeparatorView, type DateSeparatorViewModel, type DateSeparatorViewSnapshot } from "./DateSeparatorView";
1718
import * as stories from "./DateSeparatorView.stories";
1819

@@ -73,4 +74,12 @@ describe("DateSeparatorView", () => {
7374
vm.setSnapshot({ label: "Yesterday" });
7475
await waitFor(() => expect(screen.getByText("Yesterday", { selector: "h2" })).toBeInTheDocument());
7576
});
77+
78+
it("forwards refs to the trigger element", () => {
79+
const ref = createRef<HTMLDivElement>();
80+
81+
render(<DateSeparatorButton ref={ref} label="Today" />);
82+
83+
expect(ref.current).toBe(screen.getByTestId("jump-to-date-separator-button"));
84+
});
7685
});

0 commit comments

Comments
 (0)