-
-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1633 from FreeFeed/checklist
Show beautiful & interactive checkboxes at the start of text
- Loading branch information
Showing
13 changed files
with
376 additions
and
157 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import cn from 'classnames'; | ||
import { useCallback } from 'react'; | ||
import { useDispatch, useSelector } from 'react-redux'; | ||
import { saveEditingComment } from '../redux/action-creators'; | ||
import { initialAsyncState } from '../redux/async-helpers'; | ||
import { checkMark, isChecked, setCheckState } from '../utils/initial-checkbox'; | ||
import style from './initial-checkbox.module.scss'; | ||
import { Throbber } from './throbber'; | ||
import { useComment } from './post/post-comment-ctx'; | ||
|
||
export function InitialCheckbox({ checked }) { | ||
const comment = useComment(); | ||
const myId = useSelector((state) => state.user.id); | ||
|
||
const isActive = myId && comment?.createdBy === myId; | ||
|
||
return ( | ||
<> | ||
{isActive ? ( | ||
<ActiveCheckbox comment={comment} checked={checked} /> | ||
) : ( | ||
<span className={style.textBox}> | ||
[ | ||
<span aria-hidden={!checked} className={cn(!checked && style.hidden)}> | ||
{checkMark} | ||
</span> | ||
] | ||
</span> | ||
)}{' '} | ||
</> | ||
); | ||
} | ||
|
||
function ActiveCheckbox({ comment, checked }) { | ||
const updateStatus = useSelector( | ||
(state) => state.commentEditState[comment?.id]?.saveStatus ?? initialAsyncState, | ||
); | ||
const dispatch = useDispatch(); | ||
const onClick = useCallback( | ||
(e) => { | ||
if (e.clientX !== 0) { | ||
// True mouse click (not keyboard) | ||
e.target.blur(); | ||
} | ||
const newState = e.target.checked; | ||
if (isChecked(comment.body) === newState) { | ||
return; | ||
} | ||
const updatedBody = setCheckState(comment.body, newState); | ||
dispatch(saveEditingComment(comment?.id, updatedBody)); | ||
}, | ||
[comment?.body, comment?.id, dispatch], | ||
); | ||
|
||
return ( | ||
<span className={style.box}> | ||
{updateStatus.loading && <Throbber className={style.throbber} delay={0} />} | ||
<input | ||
type="checkbox" | ||
readOnly | ||
checked={checked} | ||
className={cn(style.chk, updateStatus.loading && style.hidden)} | ||
onClick={onClick} | ||
/> | ||
</span> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
input[type='checkbox'].chk { | ||
margin: 0; | ||
width: 1.1em; | ||
height: 1.1em; | ||
cursor: pointer; | ||
|
||
&:focus-visible { | ||
outline-offset: 2px; | ||
} | ||
} | ||
|
||
.box { | ||
position: relative; | ||
margin-inline-end: 0.15em; | ||
vertical-align: middle; | ||
} | ||
|
||
.throbber { | ||
position: absolute; | ||
top: 50%; | ||
left: 50%; | ||
translate: -50% -50%; | ||
} | ||
|
||
.hidden { | ||
visibility: hidden; | ||
} | ||
|
||
.textBox { | ||
white-space: nowrap; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { createContext, useContext } from 'react'; | ||
import { useSelector } from 'react-redux'; | ||
|
||
/** | ||
* @type {import('react').Context<string|null>} | ||
*/ | ||
export const commentIdContext = createContext(null); | ||
|
||
export function useComment() { | ||
const id = useContext(commentIdContext); | ||
return useSelector((state) => state.comments[id] ?? null); | ||
} | ||
|
||
/** | ||
* @type {import('react').Context<string|null>} | ||
*/ | ||
export const postIdContext = createContext(null); | ||
|
||
export function usePost() { | ||
const id = useContext(postIdContext); | ||
return useSelector((state) => state.posts[id] ?? null); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { commentIdContext, postIdContext } from './post-comment-ctx'; | ||
|
||
export function CommentProvider({ id, children }) { | ||
return <commentIdContext.Provider value={id}>{children}</commentIdContext.Provider>; | ||
} | ||
|
||
export function PostProvider({ id, children }) { | ||
return <postIdContext.Provider value={id}>{children}</postIdContext.Provider>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.