blob: 6ac986396c810631b9994df09aef310ada9c3e20 [file] [log] [blame]
Yoshie Muranaka4ee8d292020-02-20 07:29:58 -08001<template>
2 <div class="input-password-toggle-container">
3 <slot></slot>
4 <b-button
Yoshie Muranaka547b5fc2020-02-24 15:42:40 -08005 :aria-label="$t('global.ariaLabel.showPassword')"
Yoshie Muranaka4ee8d292020-02-20 07:29:58 -08006 variant="link"
7 :class="{ isVisible: isVisible }"
8 @click="toggleVisibility"
9 >
Dixsie Wolmersd2ac76d2020-08-26 17:23:45 -050010 <icon-view-off
11 v-if="isVisible"
12 aria-hidden="true"
13 :title="$t('global.ariaLabel.hidePassword')"
14 />
15 <icon-view
16 v-else
17 aria-hidden="true"
18 :title="$t('global.ariaLabel.showPassword')"
19 />
Yoshie Muranaka4ee8d292020-02-20 07:29:58 -080020 </b-button>
21 </div>
22</template>
23
24<script>
25import IconView from '@carbon/icons-vue/es/view/20';
26import IconViewOff from '@carbon/icons-vue/es/view--off/20';
27
28export default {
29 name: 'InputPasswordToggle',
30 components: { IconView, IconViewOff },
31 data() {
32 return {
33 isVisible: false
34 };
35 },
36 methods: {
37 toggleVisibility() {
38 const firstChild = this.$children[0];
39 const inputEl = firstChild ? firstChild.$el : null;
40
41 this.isVisible = !this.isVisible;
42
43 if (inputEl.nodeName === 'INPUT') {
44 inputEl.type = this.isVisible ? 'text' : 'password';
45 }
46 }
47 }
48};
49</script>
50
51<style lang="scss" scoped>
52.input-password-toggle-container {
53 position: relative;
54}
55
56.btn {
57 position: absolute;
58 right: 0;
59 top: 0;
Dixsie Wolmers5ea16782020-07-27 17:50:43 -050060 padding: 0.4rem 1rem;
Yoshie Muranaka4ee8d292020-02-20 07:29:58 -080061 svg {
62 margin-left: 0;
Derick Montague3f6710a2020-03-04 00:59:47 -060063 vertical-align: sub;
Yoshie Muranaka4ee8d292020-02-20 07:29:58 -080064 }
65}
66</style>