Skip to content

Commit

Permalink
Improve NoteOptions a11y (#134)
Browse files Browse the repository at this point in the history
  • Loading branch information
MartinRosenberg committed Oct 19, 2019
1 parent 5a0f999 commit f588c94
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 24 deletions.
27 changes: 27 additions & 0 deletions src/components/NoteOptionsButton.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import React, { KeyboardEventHandler, MouseEventHandler } from 'react'
import { Icon } from 'react-feather'

export interface NoteOptionsButtonProps {
handler: MouseEventHandler & KeyboardEventHandler
icon: Icon
text: string
}

const NoteOptionsButton: React.FC<NoteOptionsButtonProps> = props => {
const { handler, icon: IconCmp, text, ...rest } = props
return (
<div
className="nav-item"
role="button"
onClick={handler}
onKeyPress={handler}
tabIndex={0}
{...rest}
>
<IconCmp size={18} />
{text}
</div>
)
}

export default NoteOptionsButton
40 changes: 16 additions & 24 deletions src/containers/NoteOptions.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import React from 'react'
import { ArrowUp, Star, Download, Trash, X } from 'react-feather'
import { ArrowUp, Download, Star, Trash, X } from 'react-feather'
import { useDispatch } from 'react-redux'

import NoteOptionsButton from 'components/NoteOptionsButton'
import { downloadNote, getNoteTitle } from 'helpers'
import { deleteNote, toggleFavoriteNote, toggleTrashedNote } from 'slices/note'
import { NoteItem } from 'types'
Expand Down Expand Up @@ -39,35 +40,26 @@ const NoteOptions: React.FC<NoteOptionsProps> = ({ clickedNote }) => {
<nav className="note-options-nav" data-testid="note-options-nav">
{clickedNote.trash ? (
<>
<div className="nav-item" onClick={deleteNoteHandler}>
<X size={18} />
Delete permanently
</div>
<div className="nav-item" onClick={trashNoteHandler}>
<ArrowUp size={18} />
Restore from trash
</div>
<NoteOptionsButton handler={deleteNoteHandler} icon={X} text="Delete permanently" />
<NoteOptionsButton handler={trashNoteHandler} icon={ArrowUp} text="Restore from trash" />
</>
) : (
<>
<div
className="nav-item"
onClick={favoriteNoteHandler}
<NoteOptionsButton
data-cy="note-option-favorite-button"
>
<Star size={18} />
{clickedNote.favorite ? 'Remove favorite' : 'Mark as favorite'}
</div>
<div className="nav-item" onClick={trashNoteHandler} data-cy="note-option-trash-button">
<Trash size={18} />
Move to trash
</div>
handler={favoriteNoteHandler}
icon={Star}
text={clickedNote.favorite ? 'Remove favorite' : 'Mark as favorite'}
/>
<NoteOptionsButton
data-cy="note-option-trash-button"
handler={trashNoteHandler}
icon={Trash}
text="Move to trash"
/>
</>
)}
<div className="nav-item" onClick={downloadNoteHandler}>
<Download size={18} />
Download
</div>
<NoteOptionsButton handler={downloadNoteHandler} icon={Download} text="Download" />
</nav>
)
}
Expand Down

0 comments on commit f588c94

Please sign in to comment.