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

Added sidebar stickiness, icon clicking #155

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open

Added sidebar stickiness, icon clicking #155

wants to merge 5 commits into from

Conversation

AtibQur
Copy link
Contributor

@AtibQur AtibQur commented Jan 9, 2025

No description provided.

@AtibQur AtibQur requested a review from alexp-sssup January 9, 2025 13:04
let activeInfo = null; // Tracks currently visible info
let lastHoveredInfo = null; // Tracks last hovered info
let isPanelHovered = false; // Tracks if info panel is hovered
let hideTimeout = null; // Timeout for hiding info panel
Copy link
Member

Choose a reason for hiding this comment

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

I think timeouts are numerical ids, so we should initialize this with 0

function showInfo(info) {
activeInfo = info;
clearTimeout(hideTimeout); // Cancel timeout
Copy link
Member

Choose a reason for hiding this comment

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

We should reset hideTimeout to 0 to avoid clearing it multiple times. It might be also sensible to add an if(hideTimeout) before, unless we are sure the timeout is active

class="flex flex-col gap-5 shrink-0 w-80 h-full z-10 p-2 bg-neutral-600 text-gray-100 opacity-95"
class:hidden={!activeInfo}
on:mouseover={() => {
isPanelHovered = true;
Copy link
Member

Choose a reason for hiding this comment

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

Let's keep all the logic in functions above

function hideInfo() {
activeInfo = null;
hideTimeout = setTimeout(() => {
if (!isPanelHovered) {
Copy link
Member

Choose a reason for hiding this comment

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

How can this happen? We call clearTimeout when setting isPanelHovered to true

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm not very sure what you meant by this, but when we leave the panel, isPanelHovered is set to false. After the timeout got called and is over, the rest of variables get set to null and 0 which says that there is no hovering over the panel

Copy link
Member

Choose a reason for hiding this comment

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

isPanelHovered can only be false here. When set to true we also call clearTimeout, so the code will never run in the first place.

src/lib/SideBar.svelte Show resolved Hide resolved
/>
{:else}
<div class="grow" on:mouseover={(e) => showInfo(null)}></div>
<div class="grow" style="pointer-events: none;"></div>
Copy link
Member

Choose a reason for hiding this comment

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

Use tailwind CSS classes instead of a custom style

function hideInfo() {
activeInfo = null;
hideTimeout = setTimeout(() => {
if (!isPanelHovered) {
Copy link
Member

Choose a reason for hiding this comment

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

isPanelHovered can only be false here. When set to true we also call clearTimeout, so the code will never run in the first place.

@AtibQur AtibQur requested a review from alexp-sssup January 11, 2025 13:50
src/lib/SideBar.svelte Show resolved Hide resolved
src/lib/SideBar.svelte Show resolved Hide resolved
src/lib/SideBar.svelte Outdated Show resolved Hide resolved
src/lib/SideBar.svelte Show resolved Hide resolved
activeInfo = null;
if (!hideTimeout) {
hideTimeout = setTimeout(() => {
if (!isPanelHovered) {
Copy link
Member

Choose a reason for hiding this comment

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

I still think this logic is redundant. Below we should call clear timeout so that the callback is never even called if isPanelHovered is set to true.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I understand what you mean now, having the timeout outside of the if (!isPanelHovered) is redundant. I've placed it inside the if statement.

if (!hideTimeout) makes sure that when you hover too fast over an icon and the cursor moves out of the panel, you're still able to hover back within those 400ms so you can stay in the panel without it closing, otherwise it just closes the panel

I think keeping the if (!hideTimeout) would be nice for a more 'stickier' feeling but please let me know what you think about it.

@AtibQur AtibQur requested a review from alexp-sssup January 13, 2025 13:32
hideTimeout = 0;
}
// Updates activeInfo and lastHoveredInfo to track the hovered icon, then shows the panel with the hovered icon's info
if (info !== lastHoveredInfo) {
Copy link
Member

Choose a reason for hiding this comment

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

I am not seeing the rationale for lastHoveredInfo here. Maybe at some point in the past we had some grace time before switching from one panel to another?

function hideInfo() {
activeInfo = null;
if (!hideTimeout) {
Copy link
Member

Choose a reason for hiding this comment

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

Can hideTimeout ever be non-zero here? I think we reset the timeout when the panel is hovered, which means it will always be 0 when mouseleave is reached. Potentially we could use mouseenter to make this more obvious?

Copy link
Member

Choose a reason for hiding this comment

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

@AtibQur Please check if this condition is redundant. It's the last open issue.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@alexp-sssup We need to use this condition.

Without this condition, we are not able to keep the panel open when we accidentally hover out and back in to the panel.

Copy link
Member

Choose a reason for hiding this comment

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

In handleMouseEnterPanel the timeout is cleared. I am not seeing how hideTimeout can be non-zero here.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

hideTimeout can be non-zero because handleMouseLeavePanel (via hideInfo) starts a timer. When showInfo or handleMouseEnterpanel clears the timer, hideTimeout still holds the timer ID until it resets to 0. During this moment, hovering over the panel will cancel the timer before it can trigger, preventing the panel from closing.

I hope my explanations made a bit of sense, please let me know if it's still unclear.

Copy link
Member

Choose a reason for hiding this comment

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

clears the timer, hideTimeout still holds the timer ID until it resets to 0.

Are you referring to a concurrency problem here? JavaScript is not concurrent. hideTimeout is reset to 0 immediately after clearTimeout. No other event or action can happen till the previous handler is complete.

@AtibQur AtibQur requested a review from alexp-sssup January 15, 2025 10:45
function hideInfo() {
activeInfo = null;
if (!hideTimeout) {
Copy link
Member

Choose a reason for hiding this comment

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

@AtibQur Please check if this condition is redundant. It's the last open issue.

function hideInfo() {
activeInfo = null;
if (!hideTimeout) {
Copy link
Member

Choose a reason for hiding this comment

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

In handleMouseEnterPanel the timeout is cleared. I am not seeing how hideTimeout can be non-zero here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants