Skip to content

Commit f3fd81c

Browse files
committed
Extract footer icon dimming logic into hook and keep icons active while hovering footer
- remove code duplication - outside of Home, once un-alted, alt again when cursor leaves footer, not just footer__left - create local folder hooks and extract timeout logic there
1 parent 6405dba commit f3fd81c

3 files changed

Lines changed: 83 additions & 69 deletions

File tree

‎apps/editor/src/views/prompt/frontend/components/Layout/Footer/Footer.module.scss‎

Lines changed: 17 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,7 @@
1717
gap: 4px;
1818
}
1919

20-
&__discord-button,
21-
&__donate-button {
20+
&__icon-button {
2221
--size: 24px;
2322
box-sizing: border-box;
2423
display: flex;
@@ -42,44 +41,34 @@
4241
filter: brightness(1.08);
4342
text-decoration: none;
4443
}
45-
}
46-
47-
&__discord-button {
48-
background-color: #5865f2;
49-
color: #ffffff;
5044

51-
> svg {
52-
width: 16px;
53-
fill: currentColor;
54-
}
45+
&--discord {
46+
background-color: #5865f2;
47+
color: #ffffff;
5548

56-
&--alt {
57-
background-color: transparent;
58-
border-color: var(--cwc-border-color-dimmed);
59-
color: var(--cwc-text-color-dimmed);
60-
transition:
61-
background-color 0.5s ease,
62-
border-color 0.5s ease,
63-
color 0.5s ease;
49+
> svg {
50+
width: 16px;
51+
fill: currentColor;
52+
}
6453
}
65-
}
6654

67-
&__donate-button {
68-
background-color: #ffdd00;
69-
color: #000000;
55+
&--donate {
56+
background-color: #ffdd00;
57+
color: #000000;
7058

71-
&:hover {
72-
filter: brightness(0.95);
59+
&:hover {
60+
filter: brightness(0.95);
61+
}
7362
}
7463

7564
&--alt {
7665
background-color: transparent;
7766
border-color: var(--cwc-border-color-dimmed);
7867
color: var(--cwc-text-color-dimmed);
7968
transition:
80-
background-color 0.7s ease,
81-
border-color 0.7s ease,
82-
color 0.7s ease;
69+
background-color 0.5s ease,
70+
border-color 0.5s ease,
71+
color 0.5s ease;
8372
}
8473
}
8574

‎apps/editor/src/views/prompt/frontend/components/Layout/Footer/Footer.tsx‎

Lines changed: 27 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
1-
import { useContext, useEffect, useRef, useState } from 'react'
1+
import { useContext } from 'react'
22
import styles from './Footer.module.scss'
33
import { use_compacting } from '@shared/hooks'
44
import { LayoutContext } from '../../../contexts/LayoutContext'
55
import { use_translation } from '../../../i18n/use-translation'
66
import cn from 'classnames'
77
import { CompactableActionButton } from '@ui/components/editor/prompt-view/CompactableActionButton'
88
import { Icon as UiIcon } from '@ui/components/editor/common/Icon'
9-
10-
const DIMMED_ICONS_TIMEOUT = 1000
9+
import { use_dimmed_icons } from './hooks/use-dimmed-icons'
1110

1211
type Props = {
1312
on_history_click?: () => void
@@ -25,49 +24,36 @@ export const Footer: React.FC<Props> = (props) => {
2524
const apply_label = t('footer.action.apply')
2625

2726
const { container_ref, compact_step } = use_compacting()
28-
const [is_alt, set_is_alt] = useState(true)
29-
const leave_timeout_ref = useRef<ReturnType<typeof setTimeout> | null>(null)
30-
31-
const handle_mouse_enter = () => {
32-
if (leave_timeout_ref.current) {
33-
clearTimeout(leave_timeout_ref.current)
34-
leave_timeout_ref.current = null
35-
}
36-
set_is_alt(false)
37-
}
38-
39-
const handle_mouse_leave = () => {
40-
leave_timeout_ref.current = setTimeout(() => {
41-
set_is_alt(true)
42-
}, DIMMED_ICONS_TIMEOUT)
43-
}
44-
45-
useEffect(() => {
46-
return () => {
47-
if (leave_timeout_ref.current) {
48-
clearTimeout(leave_timeout_ref.current)
49-
}
50-
}
51-
}, [])
52-
53-
const handle_apply_click = () => {
54-
on_apply_click()
55-
}
27+
const {
28+
is_alt,
29+
clear_leave_timeout,
30+
handle_left_mouse_enter,
31+
handle_footer_mouse_leave
32+
} = use_dimmed_icons()
5633

5734
const is_alt_style = is_alt && !props.is_home_active
5835

36+
const get_icon_button_class = (variant_class: string) =>
37+
cn(styles['footer__icon-button'], variant_class, {
38+
[styles['footer__icon-button--alt']]: is_alt_style
39+
})
40+
5941
return (
60-
<div className={styles.footer} ref={container_ref}>
42+
<div
43+
className={styles.footer}
44+
ref={container_ref}
45+
onMouseEnter={clear_leave_timeout}
46+
onMouseLeave={handle_footer_mouse_leave}
47+
>
6148
<div
6249
className={styles.footer__left}
63-
onMouseEnter={handle_mouse_enter}
64-
onMouseLeave={handle_mouse_leave}
50+
onMouseEnter={handle_left_mouse_enter}
6551
>
6652
<a
6753
href="https://discord.gg/KJySXsrSX5"
68-
className={cn(styles['footer__discord-button'], {
69-
[styles['footer__discord-button--alt']]: is_alt_style
70-
})}
54+
className={get_icon_button_class(
55+
styles['footer__icon-button--discord']
56+
)}
7157
title="Discord"
7258
target="_blank"
7359
rel="noopener noreferrer"
@@ -76,9 +62,9 @@ export const Footer: React.FC<Props> = (props) => {
7662
</a>
7763
<button
7864
type="button"
79-
className={cn(styles['footer__donate-button'], {
80-
[styles['footer__donate-button--alt']]: is_alt_style
81-
})}
65+
className={get_icon_button_class(
66+
styles['footer__icon-button--donate']
67+
)}
8268
onClick={props.on_donate_click}
8369
title={t('header.buy-me-a-coffee')}
8470
>
@@ -104,7 +90,7 @@ export const Footer: React.FC<Props> = (props) => {
10490
/>
10591
<CompactableActionButton
10692
is_compact={compact_step >= 3}
107-
on_click={handle_apply_click}
93+
on_click={on_apply_click}
10894
title={t('footer.apply-title')}
10995
codicon="check-all"
11096
label={apply_label}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import { useEffect, useRef, useState } from 'react'
2+
3+
const DIMMED_ICONS_TIMEOUT = 1000
4+
5+
export const use_dimmed_icons = () => {
6+
const [is_alt, set_is_alt] = useState(true)
7+
const leave_timeout_ref = useRef<ReturnType<typeof setTimeout> | null>(null)
8+
9+
const clear_leave_timeout = () => {
10+
if (leave_timeout_ref.current) {
11+
clearTimeout(leave_timeout_ref.current)
12+
leave_timeout_ref.current = null
13+
}
14+
}
15+
16+
const handle_left_mouse_enter = () => {
17+
clear_leave_timeout()
18+
set_is_alt(false)
19+
}
20+
21+
const handle_footer_mouse_leave = () => {
22+
leave_timeout_ref.current = setTimeout(() => {
23+
set_is_alt(true)
24+
}, DIMMED_ICONS_TIMEOUT)
25+
}
26+
27+
useEffect(() => {
28+
return () => {
29+
clear_leave_timeout()
30+
}
31+
}, [])
32+
33+
return {
34+
is_alt,
35+
clear_leave_timeout,
36+
handle_left_mouse_enter,
37+
handle_footer_mouse_leave
38+
}
39+
}

0 commit comments

Comments
 (0)