Skip to content

📈 Open source, privacy-first web analytics — a cookie-less Google Analytics alternative with built-in error tracking and performance monitoring. Self-host it or use our cloud. Get started for free!

ui: add action statuses

Blue Mousebb5eed938be9
diff --git a/captcha.js b/captcha.js
index 189074fa1..b9d895e78 100644
--- a/captcha.js
+++ b/captcha.js
@@ -1,7 +1,39 @@
+/**
+ * Sets the provided action visible and the rest hidden
+ * @param {*} action checkbox | failure | completed | loading
+ */
+const activateAction = (action) => {
+ const actions = {
+ checkbox: document.querySelector('#checkbox'),
+ failure: document.querySelector('#failure'),
+ completed: document.querySelector('#completed'),
+ loading: document.querySelector('#loading'),
+ }
+
+ // Apply hidden class to all actions
+ actions.checkbox.classList.add('hidden')
+ actions.failure.classList.add('hidden')
+ actions.completed.classList.add('hidden')
+ actions.loading.classList.add('hidden')
+
+ // Remove hidden class from the provided action
+ actions[action].classList.remove('hidden')
+}
+
document.addEventListener('DOMContentLoaded', () => {
- const captchaBtn = document.querySelector('#challenge')
+ const captchaComponent = document.querySelector('#swetrix-captcha')
+ const branding = document.querySelector('#branding')
+
+ branding.addEventListener('click', (e) => {
+ e.stopPropagation()
+ })
+
+ captchaComponent.addEventListener('click', () => {
+ activateAction('loading')
- captchaBtn.addEventListener('click', () => {
- console.log('clicked')
+ setTimeout(() => {
+ activateAction('completed')
+ // activateAction('failure')
+ }, 2000)
})
})
diff --git a/index.html b/index.html
index 3411c09de..c2d175c49 100644
--- a/index.html
+++ b/index.html
@@ -13,7 +13,7 @@
-moz-osx-font-smoothing: grayscale;
}
- .swetrix-captcha {
+ #swetrix-captcha {
display: flex;
justify-content: center;
align-items: center;
@@ -21,12 +21,20 @@
/* 300 - 20px (padding) */
width: 280px;
- background-color: #f9f9f9;
+ /* bg-gray-100 */
+ background-color: #f9fafb;
border: 1px solid #e9e9e9;
height: 65px;
user-select: none;
padding-left: 10px;
padding-right: 10px;
+
+ cursor: pointer;
+ }
+
+ #swetrix-captcha:hover {
+ /* bg-gray-200 */
+ background-color: #f3f4f6;
}
#challenge {
@@ -42,12 +50,6 @@
margin-right: 10px;
}
- #status {
- font-size: 16px;
- color: #0f0f0f;
- /* font-weight: 400; */
- }
-
#checkbox {
background-color: #fff;
border: 1px solid #e9e9e9;
@@ -56,6 +58,16 @@
border-radius: 3px;
}
+ #status {
+ font-size: 16px;
+ color: #0f0f0f;
+ /* font-weight: 400; */
+ }
+
+ .hidden {
+ display: none !important;
+ }
+
#branding {
display: flex;
flex-direction: column;
@@ -81,18 +93,68 @@
content: "-";
margin: 0 2px;
}
+
+ #action svg {
+ width: 28px;
+ height: 28px;
+ }
+
+ #failure>svg {
+ /* bg-red-500 */
+ color: #d6292a;
+ }
+
+ #completed>svg {
+ /* bg-green-600 */
+ color: #16a24c;
+ }
+
+ #completed, #failure {
+ display: flex;
+ align-items: center;
+ justify-content: center;
+ }
+
+ /* Loading indicator */
+ @keyframes spin {
+ 0% {
+ transform: rotate(0deg);
+ }
+
+ 100% {
+ transform: rotate(360deg);
+ }
+ }
+
+ #loading {
+ border-radius: 50%;
+ width: 17px;
+ height: 17px;
+ border: 4px solid #616161;
+ border-top-color: #fff;
+ animation: spin 1s infinite linear;
+ }
</style>
<script src="captcha.js" defer></script>
</head>
<body>
- <div class="swetrix-captcha">
+ <div id="swetrix-captcha">
<div id="challenge">
<div id="action">
- <!-- Can contain a checkbox itself / a red cross (with a retry action) / a green mark -->
-
- <!-- Checkbox -->
+ <!-- Can contain a checkbox itself / a red cross (with a retry action) / a green check mark -->
<div id="checkbox"></div>
+ <div id="failure" class="hidden">
+ <svg fill="transparent" fill-opacity="0" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor">
+ <path stroke-linecap="round" stroke-linejoin="round" d="M6 18L18 6M6 6l12 12" />
+ </svg>
+ </div>
+ <div id="completed" class="hidden">
+ <svg fill="transparent" fill-opacity="0" viewBox="0 0 24 24" stroke-width="2" stroke="currentColor">
+ <path stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5" />
+ </svg>
+ </div>
+ <div id="loading" class="hidden"></div>
</div>
<div id="status">
<!-- TODO: I am human (default / success) / Verifying / Failure (retry action) -->