Skip to content

Commit

Permalink
feat: scroll to feedback view on view change (#2716)
Browse files Browse the repository at this point in the history
  • Loading branch information
Mike-Heneghan authored Feb 2, 2024
1 parent 439edc5 commit 12b78fd
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 4 deletions.
18 changes: 16 additions & 2 deletions editor.planx.uk/src/components/Feedback/MoreInfoFeedback.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
getInternalFeedbackMetadata,
insertFeedbackMutation,
} from "lib/feedback";
import React, { useState } from "react";
import React, { useEffect, useRef, useState } from "react";
import FeedbackOption from "ui/public/FeedbackOption";

import { FeedbackFormInput, UserFeedback } from ".";
Expand All @@ -36,6 +36,16 @@ const MoreInfoFeedbackComponent: React.FC = () => {
const [currentFeedbackView, setCurrentFeedbackView] =
useState<View>("yes/no");
const [feedbackOption, setFeedbackOption] = useState<Sentiment | null>(null);
const feedbackComponentRef = useRef<HTMLDivElement | null>(null);

useEffect(() => {
if (currentFeedbackView === "input") {
feedbackComponentRef.current?.scrollIntoView({
behavior: "smooth",
block: "start",
});
}
}, [currentFeedbackView]);

const handleFeedbackOptionClick = (event: Sentiment) => {
switch (event) {
Expand Down Expand Up @@ -142,7 +152,11 @@ const MoreInfoFeedbackComponent: React.FC = () => {
}
}

return <MoreInfoFeedbackView />;
return (
<Box ref={feedbackComponentRef}>
<MoreInfoFeedbackView />
</Box>
);
};

export default MoreInfoFeedbackComponent;
29 changes: 27 additions & 2 deletions editor.planx.uk/src/components/Feedback/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import {
} from "lib/feedback";
import { useStore } from "pages/FlowEditor/lib/store";
import { BackButton } from "pages/Preview/Questions";
import React, { useEffect, useState } from "react";
import React, { useEffect, useRef, useState } from "react";
import { usePrevious } from "react-use";
import FeedbackOption from "ui/public/FeedbackOption";

Expand Down Expand Up @@ -98,6 +98,27 @@ const Feedback: React.FC = () => {
}
}, [breadcrumbs]);

const feedbackComponentRef = useRef<HTMLDivElement | null>(null);

const shouldScrollToView = () => {
switch (currentFeedbackView) {
case "banner":
case "thanks":
return false;
default:
return true;
}
};

useEffect(() => {
if (shouldScrollToView()) {
feedbackComponentRef.current?.scrollIntoView({
behavior: "smooth",
block: "start",
});
}
}, [currentFeedbackView]);

function handleFeedbackViewClick(event: ClickEvents) {
switch (event) {
case "close":
Expand Down Expand Up @@ -364,7 +385,11 @@ const Feedback: React.FC = () => {
}
}

return <Feedback />;
return (
<Box ref={feedbackComponentRef}>
<Feedback />
</Box>
);
};

export default Feedback;

0 comments on commit 12b78fd

Please sign in to comment.