@@ -6,6 +6,7 @@ export default {
66 ExclamationTriangleIcon,
77 },
88 data : () => ({
9+ focusActive: false ,
910 isOpen: false ,
1011 title: null ,
1112 message: null ,
@@ -20,10 +21,13 @@ export default {
2021 this .isOpen = true ;
2122 },
2223 close () {
24+ document .removeEventListener (' keyup' , this .$el .escapeEventHandler );
25+
2326 this .isOpen = false ;
27+ this .focusActive = false ;
28+ this .showCancel = true ;
2429 },
2530 show (options = {}) {
26- document .removeEventListener (' keyup' , this .$el .escapeEventHandler );
2731
2832 this .title = options .title ;
2933 this .message = options .message ;
@@ -35,19 +39,29 @@ export default {
3539 }
3640
3741 if (options .disableCancel ) {
38- this .showCancel = false ;
42+ this .showCancel = false ;
43+ this .$el .escapeEventHandler = (evt ) => {
44+ if (evt .code === ' Escape' ) {
45+ this .confirm ();
46+ }
47+ };
3948 } else {
4049 // hitting escape is the same as cancelling
4150 this .$el .escapeEventHandler = (evt ) => {
4251 if (evt .code === ' Escape' ) {
4352 this .decline ();
4453 }
4554 };
46-
47- document .addEventListener (' keyup' , this .$el .escapeEventHandler );
4855 }
4956
57+ document .addEventListener (' keyup' , this .$el .escapeEventHandler );
58+
5059 this .open ();
60+
61+ this .$nextTick (() => {
62+ this .focusActive = true ;
63+ });
64+
5165 return new Promise ((resolve ,reject ) => {
5266 this .resolvePromise = resolve;
5367 this .rejectPromise = reject;
@@ -66,29 +80,31 @@ export default {
6680 </script >
6781
6882<template >
69- <div v-show =" isOpen" class =" fixed rounded-lg bg-gray-800 flex flex-col justify-start w-full h-full inset-0 z-40 bg-opacity-80" >
70- <div class =" bg-grey-800 h-full w-full flex justify-center items-center drop-shadow-xl" >
71- <div class =" bg-gray-900 max-w-sm max-h-[calc(1/2*100%)] rounded-lg" >
72- <div class =" p-4 rounded-t flex-none flex justify-between items-top border-b border-solid border-slate-600" >
73- <div class =" flex items-center" >
74- <ExclamationTriangleIcon class =" h-6 w-6 text-pink-500 mr-2" />
75- <h3 class =" text-base font-semibold lg:text-lg text-white pt-0 mt-0" >{{ title }}</h3 >
83+ <focus-trap v-model:active =" focusActive" :initial-focus =" () => $refs.confirm" >
84+ <div v-show =" isOpen" class =" fixed rounded-lg bg-gray-800 flex flex-col justify-start w-full h-full inset-0 z-40 bg-opacity-80" >
85+ <div class =" bg-grey-800 h-full w-full flex justify-center items-center drop-shadow-xl" >
86+ <div class =" bg-gray-900 max-w-sm max-h-[calc(1/2*100%)] rounded-lg" >
87+ <div class =" p-4 rounded-t flex-none flex justify-between items-top border-b border-solid border-slate-600" >
88+ <div class =" flex items-center" >
89+ <ExclamationTriangleIcon class =" h-6 w-6 text-pink-500 mr-2" />
90+ <h3 class =" text-base font-semibold lg:text-lg text-white pt-0 mt-0" >{{ title }}</h3 >
91+ </div >
7692 </div >
77- </ div >
78- <div class =" p-4 rounded-t flex-none flex justify-between items-top border-b border-solid border-slate-600 " >
79- < div class =" flex items-center " >
80- <p class = " text-white " >{{ message }}</ p >
93+ < div class = " p-4 rounded-t flex-none flex justify-between items-top border-b border-solid border-slate-600 " >
94+ <div class =" flex items-center " >
95+ < p class =" text-white " >{{ message }}</ p >
96+ </ div >
8197 </div >
82- </ div >
83- <div class =" p-6 rounded-b border-solid border-slate-600 flex-none " >
84- < div class =" flex justify-end " >
85- <button v-if = " showCancel " @click =" decline " type =" button" class =" inline-flex items-center w-full justify-center rounded-full border border-pink-600 px-8 py-2 text-base font-medium text-gray-400 hover:text- white shadow-sm hover:bg-pink-600 focus:outline-none sm:ml-3 sm:w-auto sm:text-sm" >{{ cancelMessage }}</button >
86- <button @click = " confirm " type = " button " class = " inline-flex items-center w-full justify-center rounded-full border border-transparent bg-pink-600 px-8 py-2 text-base font-medium text-white shadow-sm hover:bg-pink-700 focus:outline-none sm:ml-3 sm:w-auto sm:text-sm " >{{ okMessage }}</ button >
98+ < div class = " p-6 rounded-b border-solid border-slate-600 flex-none " >
99+ <div class =" flex justify-end " >
100+ < button v-if = " showCancel " @click = " decline " type = " button " class =" inline- flex items-center w-full justify-center rounded-full border border-pink-600 px-8 py-2 text-base font-medium text-gray-400 hover:text-white shadow-sm hover:bg-pink-600 focus:outline-none sm:ml-3 sm:w-auto sm:text-sm focus:outline-none focus:ring focus:ring-pink-800 " >{{ cancelMessage }}</ button >
101+ <button ref = " confirm " @click =" confirm " type =" button" class =" inline-flex items-center w-full justify-center rounded-full border border-transparent bg- pink-600 px-8 py-2 text-base font-medium text-white shadow-sm hover:bg-pink-700 focus:outline-none sm:ml-3 sm:w-auto sm:text-sm focus:outline-none focus:ring focus:ring-pink-800 " >{{ okMessage }}</button >
102+ </ div >
87103 </div >
88104 </div >
89105 </div >
90106 </div >
91- </div >
107+ </focus-trap >
92108</template >
93109
94110
0 commit comments