Skip to content

Commit cc3da50

Browse files
authored
feat: adjust like button behaviour (#79)
### Adjust Like Button Behavior and Enhance onClick Timing #### Changes: - Improved behaviour of the like button to ensure the timing function works as intended. - Adjusted the timing of the `onClick` function to enhance user experience - Make it possible to disable like button (e.g. user not logged in) #### Impact: These adjustments provide a smoother user experience. Export Toggle Adding Upload Icon
1 parent dc78c47 commit cc3da50

File tree

7 files changed

+58
-5
lines changed

7 files changed

+58
-5
lines changed

src/components/icons/icon-simple.stories.tsx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import { EyeIcon } from './eye';
1414
import { SettingsIcon } from './settings';
1515
import { LogoutIcon } from './logout';
1616
import { CalendarIcon } from './calendar';
17+
import { UploadIcon } from './upload';
1718
import { IconProps } from './iconUtils';
1819

1920
export const iconComponents = {
@@ -32,6 +33,7 @@ export const iconComponents = {
3233
Settings: SettingsIcon,
3334
Logout: LogoutIcon,
3435
Calendar: CalendarIcon,
36+
Upload: UploadIcon,
3537
};
3638

3739
const defaultArgs: IconProps = {
@@ -60,6 +62,7 @@ export const Eye = createTemplate(iconComponents.Eye);
6062
export const Settings = createTemplate(iconComponents.Settings);
6163
export const Logout = createTemplate(iconComponents.Logout);
6264
export const Calendar = createTemplate(iconComponents.Calendar);
65+
export const Upload = createTemplate(iconComponents.Upload);
6366

6467
export default {
6568
title: 'Components/Icons',

src/components/icons/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,4 @@ export { CalendarIcon } from './calendar';
1616
export { HeartIcon } from './heart';
1717
export { ReplyIcon } from './reply';
1818
export { ArrowIcon } from './arrow';
19+
export { UploadIcon } from './upload';

src/components/icons/upload.tsx

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import React, { FC } from 'react';
2+
import { getSizeClass, getColorClass, IconProps } from './iconUtils';
3+
4+
export const UploadIcon: FC<IconProps> = ({ size, color }) => {
5+
const svgClasses = getSizeClass(size);
6+
const pathClasses = getColorClass(color);
7+
8+
return (
9+
<svg
10+
xmlns="http://www.w3.org/2000/svg"
11+
width="16"
12+
height="16"
13+
viewBox="0 0 16 16"
14+
fill="none"
15+
className={svgClasses}>
16+
<path
17+
d="M13 9.00003C12.7348 9.00003 12.4804 9.10539 12.2929 9.29292C12.1054 9.48046 12 9.73481 12 10V12H2V10C2 9.73481 1.89464 9.48046 1.70711 9.29292C1.51957 9.10539 1.26522 9.00003 1 9.00003C0.734784 9.00003 0.48043 9.10539 0.292893 9.29292C0.105357 9.48046 0 9.73481 0 10V12C0 12.5305 0.210714 13.0392 0.585786 13.4142C0.960859 13.7893 1.46957 14 2 14H12C12.5304 14 13.0391 13.7893 13.4142 13.4142C13.7893 13.0392 14 12.5305 14 12V10C14 9.73481 13.8946 9.48046 13.7071 9.29292C13.5196 9.10539 13.2652 9.00003 13 9.00003Z"
18+
fill="#475569"
19+
className={pathClasses}
20+
/>
21+
<path
22+
d="M3.707 5.70703L6 3.41403V9.00003C6 9.26525 6.10536 9.5196 6.29289 9.70714C6.48043 9.89467 6.73478 10 7 10C7.26522 10 7.51957 9.89467 7.70711 9.70714C7.89464 9.5196 8 9.26525 8 9.00003V3.41403L10.293 5.70703C10.4816 5.88919 10.7342 5.98998 10.9964 5.9877C11.2586 5.98543 11.5094 5.88026 11.6948 5.69485C11.8802 5.50944 11.9854 5.25863 11.9877 4.99643C11.99 4.73423 11.8892 4.48163 11.707 4.29303L7.707 0.293031C7.51947 0.10556 7.26516 0.000244141 7 0.000244141C6.73484 0.000244141 6.48053 0.10556 6.293 0.293031L2.293 4.29303C2.11084 4.48163 2.01005 4.73423 2.01233 4.99643C2.0146 5.25863 2.11977 5.50944 2.30518 5.69485C2.49059 5.88026 2.7414 5.98543 3.0036 5.9877C3.2658 5.98998 3.5184 5.88919 3.707 5.70703Z"
23+
fill="#475569"
24+
className={pathClasses}
25+
/>
26+
</svg>
27+
);
28+
};

src/components/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,4 @@ export { AvatarEdit } from './avatar';
1313
export { LikeButton } from './like-button';
1414
export { CopyLinkButton } from './copy-link-button';
1515
export { CommentButton } from './comment-button';
16+
export { Toggle } from './toggle';

src/components/like-button/like-button.tsx

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
import React, { useState, useEffect } from 'react';
1+
import React, { useEffect, useState } from 'react';
22
import { Toggle } from '../toggle/toggle';
33
import { HeartIcon } from '../icons';
44

55
interface LikeButtonProps {
66
count: number;
77
isAlreadyLiked: boolean;
8+
isDisabled?: boolean;
89
labels: {
910
zero: string;
1011
transition: string;
@@ -31,10 +32,16 @@ const getLikesText = (
3132

3233
let timer: NodeJS.Timeout;
3334

34-
export const LikeButton: React.FC<LikeButtonProps> = ({ count, isAlreadyLiked, labels, onClick }) => {
35+
export const LikeButton: React.FC<LikeButtonProps> = ({
36+
count,
37+
isAlreadyLiked,
38+
isDisabled = false,
39+
labels,
40+
onClick,
41+
}) => {
3542
const [isLiked, setIsLiked] = useState(isAlreadyLiked);
3643
const [label, setLabel] = useState(getLikesText(count, labels));
37-
const [disabled, setDisabled] = useState(false);
44+
const [disabled, setDisabled] = useState(isDisabled);
3845

3946
useEffect(() => {
4047
if (isLiked && !isAlreadyLiked) {
@@ -44,16 +51,23 @@ export const LikeButton: React.FC<LikeButtonProps> = ({ count, isAlreadyLiked, l
4451
timer = setTimeout(() => {
4552
setLabel(getLikesText(count, labels));
4653
setDisabled(false);
54+
onClick();
4755
}, 1500);
4856

4957
return () => {
5058
if (timer) {
5159
clearTimeout(timer);
5260
}
5361
};
62+
} else if (!isLiked && isAlreadyLiked) {
63+
onClick();
5464
}
5565
}, [isLiked]);
5666

67+
useEffect(() => {
68+
setDisabled(isDisabled);
69+
}, [isDisabled]);
70+
5771
useEffect(() => {
5872
setIsLiked(isAlreadyLiked);
5973
setLabel(getLikesText(count, labels));
@@ -62,7 +76,6 @@ export const LikeButton: React.FC<LikeButtonProps> = ({ count, isAlreadyLiked, l
6276
const handleLikeClick = () => {
6377
if (!disabled) {
6478
setIsLiked(!isLiked);
65-
onClick();
6679
}
6780
};
6881

src/components/toggle/toggle.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
import React, { FC, ReactElement, useState } from 'react';
21
import classnames from 'classnames';
2+
import React from 'react';
3+
import { FC, ReactElement, useState } from 'react';
34

45
export type ToggleProps = {
56
label: string;

src/docs/design-tokens/icons.mdx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import {
1919
HeartIcon,
2020
ReplyIcon,
2121
ArrowIcon,
22+
UploadIcon,
2223
} from '../../components/icons';
2324

2425
<Meta title="Foundation/Icons" />
@@ -135,6 +136,11 @@ import {
135136
<ArrowIcon />
136137
</IconItem>
137138
</a>
139+
<a href="/?path=/story/components-icons--upload">
140+
<IconItem name="upload">
141+
<UploadIcon />
142+
</IconItem>
143+
</a>
138144
</IconGallery>
139145

140146
</IconGallery>

0 commit comments

Comments
 (0)