Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: scroll to feedback view on view change #2716

Merged
merged 5 commits into from
Feb 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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]);
Comment on lines +39 to +48
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A custom hook might be a slightly cleaner way of reusing this logic across both components / files?

Something like -

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

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

  return feedbackComponentRef;
};

Alternatively just a wrapper for the feedback components with this logic could work also. If this makes things more complex or there's something I'm missing here no sweat!


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;
Loading