Repository navigation
feat: Playwright module 2.1, CopyField, formatRelative and destructive confirm fix #32
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
11a66fa
5fcae4b
6f907eb
b9a1dbe
c3688b2
a3fc849
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| import { | ||
| InputGroup, | ||
| InputGroupAddon, | ||
| InputGroupButton, | ||
| InputGroupInput, | ||
| } from '@/components/ui/input-group'; | ||
| import { useT } from '@saucebase/react/i18n'; | ||
| import { Check, Copy } from 'lucide-react'; | ||
| import { useEffect, useRef, useState } from 'react'; | ||
| import { toast } from 'sonner'; | ||
|
|
||
| interface CopyFieldProps { | ||
| value: string; | ||
| testId: string; | ||
| className?: string; | ||
| } | ||
|
|
||
| export function CopyField({ value, testId, className }: CopyFieldProps) { | ||
| const t = useT(); | ||
|
|
||
| // The tick is feedback the user can see without reading the toast, so it clears | ||
| // itself on a timer rather than waiting for something else to re-render. | ||
| const [copied, setCopied] = useState(false); | ||
| const copyTimerRef = useRef<ReturnType<typeof setTimeout> | null>(null); | ||
|
|
||
| useEffect( | ||
| () => () => { | ||
| if (copyTimerRef.current) clearTimeout(copyTimerRef.current); | ||
| }, | ||
| [], | ||
| ); | ||
|
|
||
| const copy = () => { | ||
| navigator.clipboard | ||
| .writeText(value) | ||
|
Comment on lines
+34
to
+35
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Handle an unavailable Clipboard API before calling On an insecure origin where
|
||
| .then(() => { | ||
| toast.success(t('Copied to clipboard')); | ||
| if (copyTimerRef.current) clearTimeout(copyTimerRef.current); | ||
| setCopied(true); | ||
| copyTimerRef.current = setTimeout(() => setCopied(false), 2000); | ||
| }) | ||
| .catch(() => toast.error(t('Failed to copy'))); | ||
| }; | ||
|
|
||
| return ( | ||
| <InputGroup className={className}> | ||
| <InputGroupInput | ||
| readOnly | ||
| value={value} | ||
| data-testid={testId} | ||
| onFocus={(e) => e.target.select()} | ||
| /> | ||
| <InputGroupAddon align="inline-end"> | ||
| <InputGroupButton | ||
| type="button" | ||
| size="icon-xs" | ||
| aria-label={t('Copy')} | ||
| data-testid={`copy-${testId}`} | ||
| onClick={copy} | ||
| > | ||
| {copied ? <Check className="text-emerald-500" /> : <Copy />} | ||
| </InputGroupButton> | ||
| </InputGroupAddon> | ||
| </InputGroup> | ||
| ); | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| <script setup lang="ts"> | ||
| import { | ||
| InputGroup, | ||
| InputGroupAddon, | ||
| InputGroupButton, | ||
| InputGroupInput, | ||
| } from '@/components/ui/input-group'; | ||
| import { Check, Copy } from '@lucide/vue'; | ||
| import { trans } from 'laravel-vue-i18n'; | ||
| import type { HTMLAttributes } from 'vue'; | ||
| import { onUnmounted, ref } from 'vue'; | ||
| import { toast } from 'vue-sonner'; | ||
|
|
||
| const props = defineProps<{ | ||
| value: string; | ||
| testId: string; | ||
| class?: HTMLAttributes['class']; | ||
| }>(); | ||
|
|
||
| // The tick is feedback the user can see without reading the toast, so it clears itself | ||
| // on a timer rather than waiting for something else to re-render. | ||
| const copied = ref(false); | ||
| let copyTimer: ReturnType<typeof setTimeout> | null = null; | ||
|
|
||
| function copy() { | ||
| navigator.clipboard | ||
| .writeText(props.value) | ||
|
Comment on lines
+26
to
+27
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Handle an unavailable Clipboard API before calling On an insecure origin where
|
||
| .then(() => { | ||
| toast.success(trans('Copied to clipboard')); | ||
| if (copyTimer) clearTimeout(copyTimer); | ||
| copied.value = true; | ||
| copyTimer = setTimeout(() => (copied.value = false), 2000); | ||
| }) | ||
| .catch(() => toast.error(trans('Failed to copy'))); | ||
| } | ||
|
|
||
| onUnmounted(() => { | ||
| if (copyTimer) clearTimeout(copyTimer); | ||
| }); | ||
| </script> | ||
|
|
||
| <template> | ||
| <InputGroup :class="props.class"> | ||
| <!-- `model-value`, not `value`: Input binds v-model to the native input, so a | ||
| plain value attribute is overwritten by the empty model on first render. --> | ||
| <InputGroupInput | ||
| readonly | ||
| :model-value="props.value" | ||
| :data-testid="props.testId" | ||
| @focus="(e: FocusEvent) => (e.target as HTMLInputElement).select()" | ||
| /> | ||
| <InputGroupAddon align="inline-end"> | ||
| <InputGroupButton | ||
| type="button" | ||
| size="icon-xs" | ||
| :aria-label="$t('Copy')" | ||
| :data-testid="`copy-${props.testId}`" | ||
| @click="copy" | ||
| > | ||
| <Check v-if="copied" class="text-emerald-500" /> | ||
| <Copy v-else /> | ||
| </InputGroupButton> | ||
| </InputGroupAddon> | ||
| </InputGroup> | ||
| </template> | ||
Uh oh!
There was an error while loading. Please reload this page.