-
Notifications
You must be signed in to change notification settings - Fork 90
Add keyboard shortcut to toggle night mode #62
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks great :)
case event.ctrlKey && 'm': | ||
if (document.querySelector('.nightmode-toggle')) { | ||
document.querySelector('.nightmode-toggle').click(); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you move this to a separate function that is called here? It's not large right now, but it will become unmaintainable like this when we add more shortcuts.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe make keyboard-shortcuts
a folder. This file would become the index and any shortcut added would have it's function on it's own file. Something like the tree below.
source
└── features
└── keyboard-shortcuts
├── index.js
└── nightmode-toggle.js
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just pushed a version with the night mode toggle in a separate function, but the 'keyboard-shortcuts folder' option does seem like a better solution.
if (document.querySelector('#init-data')) { | ||
const initData = JSON.parse(document.querySelector('#init-data').value); | ||
const updatedShortcuts = initData.keyboardShortcuts; | ||
for (let i = 0; i < updatedShortcuts.length; i++) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
for (const [i, item] of updatedShortcuts.entries()) {
} | ||
}); | ||
|
||
if (document.querySelector('#init-data')) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You have document.querySelector('#init-data')
3 times. Better put it in a variable and reuse it.
Added shortcut for night mode. fixes sindresorhus#28
Can you add it to the feature highlights in the readme? |
Separate shortcut functions from event listener code. Shortcut descriptions for loop. No repeated calls to querySelector
One question: in the feature highlights in the readme, should I just say that the extension adds new keyboard shortcuts to twitter or should I list the new shortcuts as well (right now it's just the night mode toggle, but it would grow in the future)? |
const initDataElement = document.querySelector('#init-data'); | ||
if (initDataElement) { | ||
const initData = JSON.parse(initDataElement.value); | ||
const updatedShortcuts = initData.keyboardShortcuts; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Make sure you spread this, so you're working with a copy instead of a reference:
const updatedShortcuts = [...initData.keyboardShortcuts];
@Ricardo-Silva91 for now, I guess something like "Keyboard shortcut to toggle Nightmode (Ctrl-m)" will suffice. If we ever add more shortcuts, we can change that and either list all of them in a proper section at the |
readme.md
Outdated
@@ -31,6 +31,7 @@ We use Twitter a lot and notice many dumb annoyances we'd like to fix. So here b | |||
- Prevents DM modal from closing when (accidentally) clicking outside the modal. | |||
- Highlight your mentions in the stream | |||
- [Adds a `Likes` button to the main navbar](https://user-images.githubusercontent.com/14620121/35988497-ace9f93e-0ce5-11e8-8675-17e6ee38cd99.png) | |||
- Keyboard shortcut to toggle Night Mode (Ctrl-m). |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wrap keyboard shortcuts with the <kbd/>
tags:
- Keyboard shortcut to toggle Night Mode (<kbd>Ctrl</kbd><kbd>m</kbd>).
readme.md
Outdated
@@ -31,6 +31,7 @@ We use Twitter a lot and notice many dumb annoyances we'd like to fix. So here b | |||
- Prevents DM modal from closing when (accidentally) clicking outside the modal. | |||
- Highlight your mentions in the stream | |||
- [Adds a `Likes` button to the main navbar](https://user-images.githubusercontent.com/14620121/35988497-ace9f93e-0ce5-11e8-8675-17e6ee38cd99.png) | |||
- Keyboard shortcut to toggle Night Mode (Ctrl-m). | |||
|
|||
Tip: Twitter has a native [dark mode](https://github.com/sindresorhus/refined-twitter/issues/10). And press <kbd>Command/Ctrl</kbd> <kbd>?</kbd> to see all keyboard shortcuts. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe update this line to show the shortcut as well?
Tip: Twitter has a native [dark mode](https://github.com/sindresorhus/refined-twitter/issues/10) and you can toggle it using <kbd>Ctrl</kbd><kbd>m</kbd>. And press <kbd>Command/Ctrl</kbd> <kbd>?</kbd> to see all keyboard shortcuts.
Just don't forget to solve the conflicts on |
readme.md
Outdated
|
||
Tip: Twitter has a native [dark mode](https://github.com/sindresorhus/refined-twitter/issues/10). And press <kbd>Shift</kbd> <kbd>?</kbd> to see all keyboard shortcuts. | ||
Tip: Twitter has a native [dark mode](https://github.com/sindresorhus/refined-twitter/issues/10) and you can toggle it using <kbd>Ctrl</kbd><kbd>m</kbd>. And press <kbd>Command/Ctrl</kbd> <kbd>?</kbd> to see all keyboard shortcuts. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
And press Command/Ctrl ? to see all keyboard shortcuts.
This change is incorrect. Please see master.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Right, my mistake, sorry.
} | ||
}; | ||
|
||
export default async () => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Remove async
. It has no purpose here.
@@ -0,0 +1,55 @@ | |||
const toggleNightMode = () => { | |||
if (document.querySelector('.nightmode-toggle')) { | |||
document.querySelector('.nightmode-toggle').click(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Put it in a variable so we don't query twice.
Thanks for this awesome contribution @Ricardo-Silva91 :) |
fixes #28
Added a new 'keyboard shortcuts' feature so that more custom shortcuts could be implemented in the future.
Also adds the custom shortcuts keys and description to the 'Keyboard Shortcuts' help modal.
To toggle night mode I just programmatically clicked the night mode element, this is a bit forceful and ugly, I'm trying to find a better solution now. (still, because the navbar is always there, this seems to work).
Feedback is welcome 😄
P.S.: the shortcut used to toggle night mode is Ctrl+m