Fireship · @fireship
Angular Directives - Build a Hold-to-Delete Button
Uploaded September 26, 2018 · Length 9:28
Summary
build a hold-to-delete button with angular directives and rxjs, preventing accidental deletion
- angular
- rxjs
- hold-to-delete
- button
- directive
- javascript
- typescript
Transcript preview
The first 2 minutes of the transcript. Select a timestamp to watch that moment on YouTube.
- 0:00
[Music]
- 0:05
one design flaw that I see in a lot of
- 0:08
apps is having a delete button with no
- 0:10
option to undo or to confirm the
- 0:12
deletion first you could implement an
- 0:14
undo button but then you have some
- 0:15
complex state management issues to worry
- 0:17
about or you could trigger an alert
- 0:19
message but that's kind of annoying for
- 0:21
your users what I want to show you today
- 0:22
is how to build a hold to delete button
- 0:24
which i think is a refreshing take on
- 0:26
how to prevent accidental deletion and
- 0:28
it's a great exercise for learning how
- 0:30
to work with rxjs and angular directives
- 0:32
if you're new to the channel I can
- 0:34
subscribe and you can find the full
- 0:35
source code on angular firebase comm the
- 0:38
inspiration for this video comes from
- 0:40
Josh on our slack team who asked for a
- 0:42
more elegant way to handle deletions
- 0:44
than just showing a standard bootstrap
- 0:46
confirm modal I'm definitely not the
- 0:48
first person to come up with this idea
- 0:49
there's a number of apps that have
- 0:50
started using this functionality instead
- 0:52
of alert messages or undo buttons and
- 0:54
the idea is pretty simple we have a
- 0:56
regular delete button but the user needs
- 0:58
to hold it for a certain amount of time
- 1:00
before the actual deletion is executed
- 1:02
the way this works is we have an angular
- 1:04
directive that can attach itself to any
- 1:06
Dom element or other component then
- 1:09
track the time difference between the
- 1:10
mousedown event and the mouse up event
- 1:12
then we can emit a custom event every
- 1:15
tenth of a second so we can update our
- 1:16
progress bar while the user is holding
- 1:18
the button down I'm just in a regular
- 1:20
angular app and I have angular fire
- 1:22
installed just so we have something
- 1:23
meaningful to delete in an actual
- 1:25
database but this lesson is primarily
- 1:27
focused on rxjs and angular the first
- 1:30
thing we'll do is generate our directive
- 1:31
by running ng generate directive
- 1:33
holdable from the command line we'll be
- 1:36
able to attach this directive to a
- 1:37
button and then keep track of gits
- 1:39
internal state and then emit events up
- 1:41
to the parent component to do that will
- 1:43
import host listener event emitter and
- 1:45
output from angular core we'll also
- 1:48
bring in a few things from rxjs that
- 1:50
we'll see here in a minute the first
- 1:52
thing we'll do is set up a custom event
- 1:53
and this is the data that we want to
- 1:55
emit up to the parent component this
- 1:58
event will emit the number of
- 1:59
milliseconds that a user has held the


