-
Notifications
You must be signed in to change notification settings - Fork 62
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
4 changed files
with
182 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
<!-- | ||
Copyright 2023 ODK Central Developers | ||
See the NOTICE file at the top-level directory of this distribution and at | ||
https://github.com/getodk/central-frontend/blob/master/NOTICE. | ||
|
||
This file is part of ODK Central. It is subject to the license terms in | ||
the LICENSE file found in the top-level directory of this distribution and at | ||
https://www.apache.org/licenses/LICENSE-2.0. No part of ODK Central, | ||
including this file, may be copied, modified, propagated, or distributed | ||
except according to the terms contained in the LICENSE file. | ||
--> | ||
|
||
<!-- Parts of this component are based on the npm package | ||
vue-password-strength-meter 1.7.2, which uses the MIT license. | ||
https://github.com/apertureless/vue-password-strength-meter --> | ||
<template> | ||
<div class="password-strength"> | ||
<div :data-score="score"></div> | ||
</div> | ||
</template> | ||
|
||
<script setup> | ||
import { computed } from 'vue'; | ||
|
||
const props = defineProps({ | ||
password: { | ||
type: String, | ||
required: true | ||
} | ||
}); | ||
|
||
const score = computed(() => { | ||
const { length } = props.password; | ||
if (length < 8) return 1; | ||
if (length < 10) return 2; | ||
if (length < 12) return 3; | ||
if (length < 14) return 4; | ||
return 5; | ||
}); | ||
</script> | ||
|
||
<style lang="scss"> | ||
@use 'sass:color'; | ||
@import '../assets/scss/mixins'; | ||
|
||
.password-strength { | ||
background-color: #ddd; | ||
float: right; | ||
height: 2px; | ||
margin-bottom: 20px; | ||
margin-top: 10px; | ||
position: relative; | ||
width: 50%; | ||
|
||
// Use the borders of two pseduo-elements to create 4 blank spaces (gaps), | ||
// resulting in 5 bars. | ||
$between-bars: 5px; | ||
$bar-width: calc(20% - #{4 * $between-bars / 5}); | ||
&::before, &::after { | ||
background-color: transparent; | ||
border-color: #fff; | ||
border-style: solid; | ||
border-width: 0 $between-bars 0 $between-bars; | ||
box-sizing: content-box; | ||
content: ''; | ||
display: block; | ||
height: 100%; | ||
position: absolute; | ||
top: 0; | ||
width: $bar-width; | ||
z-index: 1; | ||
} | ||
&::before { left: $bar-width; } | ||
&::after { right: $bar-width; } | ||
|
||
[data-score] { | ||
height: 100%; | ||
transition: width 0.5s ease-in-out, background-color 0.25s; | ||
} | ||
[data-score="1"] { | ||
background-color: $color-danger; | ||
// 20% is somewhere between the two bars (it is greater than $bar-width and | ||
// less than $bar-width + $between-bars). But since the pseudo-elements have | ||
// a higher z-index, only $bar-width of the background color will be | ||
// visible (as desired). | ||
width: 20%; | ||
} | ||
[data-score="2"] { | ||
background-color: color.mix($color-danger, $color-warning); | ||
width: 40%; | ||
} | ||
[data-score="3"] { | ||
background-color: $color-warning; | ||
width: 60%; | ||
} | ||
[data-score="4"] { | ||
background-color: color.mix($color-warning, $color-success); | ||
width: 80%; | ||
} | ||
[data-score="5"] { | ||
background-color: $color-success; | ||
width: 100%; | ||
} | ||
} | ||
</style> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import PasswordStrength from '../../src/components/password-strength.vue'; | ||
|
||
import { mount } from '../util/lifecycle'; | ||
|
||
describe('PasswordStrength', () => { | ||
const cases = [ | ||
['', 1], | ||
['a', 1], | ||
['aaaaaaa', 1], | ||
['aaaaaaaa', 2], | ||
['aaaaaaaaaa', 3], | ||
['aaaaaaaaaaaa', 4], | ||
['aaaaaaaaaaaaaa', 5] | ||
]; | ||
for (const [password, score] of cases) { | ||
it(`sets data-score to ${score} if the password is '${password}'`, () => { | ||
const component = mount(PasswordStrength, { | ||
props: { password } | ||
}); | ||
const data = component.get('[data-score]').attributes('data-score'); | ||
data.should.equal(score.toString()); | ||
}); | ||
} | ||
}); |