Fireship · @fireship

Angular Directives - Build a Hold-to-Delete Button

Uploaded September 26, 2018 · Length 9:28

Angular Directives - Build a Hold-to-Delete Button

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.

  1. 0:00

    [Music]

  2. 0:05

    one design flaw that I see in a lot of

  3. 0:08

    apps is having a delete button with no

  4. 0:10

    option to undo or to confirm the

  5. 0:12

    deletion first you could implement an

  6. 0:14

    undo button but then you have some

  7. 0:15

    complex state management issues to worry

  8. 0:17

    about or you could trigger an alert

  9. 0:19

    message but that's kind of annoying for

  10. 0:21

    your users what I want to show you today

  11. 0:22

    is how to build a hold to delete button

  12. 0:24

    which i think is a refreshing take on

  13. 0:26

    how to prevent accidental deletion and

  14. 0:28

    it's a great exercise for learning how

  15. 0:30

    to work with rxjs and angular directives

  16. 0:32

    if you're new to the channel I can

  17. 0:34

    subscribe and you can find the full

  18. 0:35

    source code on angular firebase comm the

  19. 0:38

    inspiration for this video comes from

  20. 0:40

    Josh on our slack team who asked for a

  21. 0:42

    more elegant way to handle deletions

  22. 0:44

    than just showing a standard bootstrap

  23. 0:46

    confirm modal I'm definitely not the

  24. 0:48

    first person to come up with this idea

  25. 0:49

    there's a number of apps that have

  26. 0:50

    started using this functionality instead

  27. 0:52

    of alert messages or undo buttons and

  28. 0:54

    the idea is pretty simple we have a

  29. 0:56

    regular delete button but the user needs

  30. 0:58

    to hold it for a certain amount of time

  31. 1:00

    before the actual deletion is executed

  32. 1:02

    the way this works is we have an angular

  33. 1:04

    directive that can attach itself to any

  34. 1:06

    Dom element or other component then

  35. 1:09

    track the time difference between the

  36. 1:10

    mousedown event and the mouse up event

  37. 1:12

    then we can emit a custom event every

  38. 1:15

    tenth of a second so we can update our

  39. 1:16

    progress bar while the user is holding

  40. 1:18

    the button down I'm just in a regular

  41. 1:20

    angular app and I have angular fire

  42. 1:22

    installed just so we have something

  43. 1:23

    meaningful to delete in an actual

  44. 1:25

    database but this lesson is primarily

  45. 1:27

    focused on rxjs and angular the first

  46. 1:30

    thing we'll do is generate our directive

  47. 1:31

    by running ng generate directive

  48. 1:33

    holdable from the command line we'll be

  49. 1:36

    able to attach this directive to a

  50. 1:37

    button and then keep track of gits

  51. 1:39

    internal state and then emit events up

  52. 1:41

    to the parent component to do that will

  53. 1:43

    import host listener event emitter and

  54. 1:45

    output from angular core we'll also

  55. 1:48

    bring in a few things from rxjs that

  56. 1:50

    we'll see here in a minute the first

  57. 1:52

    thing we'll do is set up a custom event

  58. 1:53

    and this is the data that we want to

  59. 1:55

    emit up to the parent component this

  60. 1:58

    event will emit the number of

  61. 1:59

    milliseconds that a user has held the