Skip to content

Commit

Permalink
feat: 🎸 optimize ux of tooltip component
Browse files Browse the repository at this point in the history
  • Loading branch information
Saul-Mirone committed Dec 11, 2023
1 parent 03b2d13 commit 9c8b145
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 5 deletions.
6 changes: 6 additions & 0 deletions e2e/src/link-tooltip/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

milkdown-link-preview {
& > .link-preview {
height: 42px;
display: flex;
justify-content: center;
padding: 8px;
Expand Down Expand Up @@ -71,7 +72,9 @@ milkdown-link-preview {

milkdown-link-edit {
& > .link-edit {
height: 42px;
display: flex;
justify-content: center;
padding: 8px 12px;
background: bisque;
gap: 8px;
Expand All @@ -92,6 +95,9 @@ milkdown-link-edit {
&:hover {
background: burlywood;
}
&.hidden {
visibility: hidden;
}
}
}
}
22 changes: 17 additions & 5 deletions packages/components/src/link-tooltip/edit/edit-component.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/* Copyright 2021, Milkdown by Mirone. */
import type { Component } from 'atomico'
import { html, useRef } from 'atomico'
import { html, useEffect, useRef, useState } from 'atomico'
import clsx from 'clsx'
import type { LinkTooltipConfig } from '../slices'

export interface LinkEditProps {
Expand All @@ -17,16 +18,26 @@ export const linkEditComponent: Component<LinkEditProps> = ({
config,
}) => {
const linkInput = useRef<HTMLInputElement>()
const [link, setLink] = useState(src)

useEffect(() => {
setLink(src ?? '')
}, [src])

const onConfirmEdit = () => {
onConfirm?.(linkInput.current?.value ?? '')
}

const onKeydown = (e: KeyboardEvent) => {
if (e.key === 'Enter')
e.stopPropagation()
if (e.key === 'Enter') {
onConfirm?.(linkInput.current?.value ?? '')
if (e.key === 'Escape')
e.preventDefault()
}
if (e.key === 'Escape') {
onCancel?.()
e.preventDefault()
}
}

return html`
Expand All @@ -37,9 +48,10 @@ export const linkEditComponent: Component<LinkEditProps> = ({
placeholder=${config?.inputPlaceholder}
ref=${linkInput}
onkeydown=${onKeydown}
value=${src}
oninput=${(e: InputEvent) => setLink((e.target as HTMLInputElement).value)}
value=${link}
/>
<span class="button confirm" onclick=${onConfirmEdit}>
<span class=${clsx('button confirm', link.length === 0 && 'hidden')} onclick=${onConfirmEdit}>
${config?.confirmButton()}
</span>
</div>
Expand Down
2 changes: 2 additions & 0 deletions packages/components/src/link-tooltip/edit/edit-view.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ export class LinkEditTooltip implements PluginView {
tippyOptions: {
arrow: false,
appendTo: document.body,
delay: 0,
duration: 0,
onHidden: () => {
this.#content.update().catch((e) => {
throw e
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export class LinkPreviewTooltip implements PluginView {
appendTo: document.body,
arrow: false,
delay: 0,
duration: 0,
},
})

Expand Down

0 comments on commit 9c8b145

Please sign in to comment.