share
import "drab/share/define";
<drab-share value="https://drab.robino.dev">
<button data-trigger type="button" class="gap-1.5">
Share
<span data-content>
<!-- up-arrow-on-square -->
<svg
xmlns="http://www.w3.org/2000/svg"
viewBox="0 0 20 20"
fill="currentColor"
class="size-5"
>
<path
fill-rule="evenodd"
d="M13.75 7h-3V3.66l1.95 2.1a.75.75 0 1 0 1.1-1.02l-3.25-3.5a.75.75 0 0 0-1.1 0L6.2 4.74a.75.75 0 0 0 1.1 1.02l1.95-2.1V7h-3A2.25 2.25 0 0 0 4 9.25v7.5A2.25 2.25 0 0 0 6.25 19h7.5A2.25 2.25 0 0 0 16 16.75v-7.5A2.25 2.25 0 0 0 13.75 7Zm-3 0h-1.5v5.25a.75.75 0 0 0 1.5 0V7Z"
clip-rule="evenodd"
/>
</svg>
</span>
<template data-swap>
<!-- clipboard-document-check -->
<svg
xmlns="http://www.w3.org/2000/svg"
viewBox="0 0 20 20"
fill="currentColor"
class="size-5"
>
<path
fill-rule="evenodd"
d="M18 5.25a2.25 2.25 0 0 0-2.012-2.238A2.25 2.25 0 0 0 13.75 1h-1.5a2.25 2.25 0 0 0-2.238 2.012c-.875.092-1.6.686-1.884 1.488H11A2.5 2.5 0 0 1 13.5 7v7h2.25A2.25 2.25 0 0 0 18 11.75v-6.5ZM12.25 2.5a.75.75 0 0 0-.75.75v.25h3v-.25a.75.75 0 0 0-.75-.75h-1.5Z"
clip-rule="evenodd"
/>
<path
fill-rule="evenodd"
d="M3 6a1 1 0 0 0-1 1v10a1 1 0 0 0 1 1h8a1 1 0 0 0 1-1V7a1 1 0 0 0-1-1H3Zm6.874 4.166a.75.75 0 1 0-1.248-.832l-2.493 3.739-.853-.853a.75.75 0 0 0-1.06 1.06l1.5 1.5a.75.75 0 0 0 1.154-.114l3-4.5Z"
clip-rule="evenodd"
/>
</svg>
</template>
</button>
</drab-share>
Overview
Defined in: share/index.ts:8
Uses the Navigator API to share a url. If share
is not supported, falls back to copy the text instead.
Extends
BaseCopy
Constructors
new Share()
new Share():
Share
Defined in: share/index.ts:9
Returns
Overrides
BaseCopy.constructor
Accessors
event
Get Signature
get event(): keyof
HTMLElementEventMap
Defined in: base/index.ts:42
Event for the trigger
to execute.
For example, set to "mouseover"
to execute the event when the user hovers the mouse over the trigger
, instead of when they click it.
Default
"click";
Returns
keyof HTMLElementEventMap
Set Signature
set event(
value
):void
Defined in: base/index.ts:46
Parameters
value
keyof HTMLElementEventMap
Returns
void
Inherited from
BaseCopy.event
value
Get Signature
get value():
string
Defined in: base/copy/index.ts:15
The value to copy or share.
Default
"" the empty string
Returns
string
Set Signature
set value(
value
):void
Defined in: base/copy/index.ts:19
Parameters
value
string
Returns
void
Inherited from
BaseCopy.value
Methods
announce()
announce(
message
):void
Defined in: base/index.ts:53
Parameters
message
string
message to announce to screen readers
Returns
void
Inherited from
BaseCopy.announce
connectedCallback()
connectedCallback():
void
Defined in: base/index.ts:171
Called when custom element is added to the page.
Returns
void
Inherited from
BaseCopy.connectedCallback
copy()
copy(
text
):Promise
<void
>
Defined in: base/copy/index.ts:27
Copies the text
.
Parameters
text
string
= ...
The text
to share
Returns
Promise
<void
>
Inherited from
BaseCopy.copy
destroy()
destroy():
void
Defined in: base/index.ts:178
Passed into disconnectedCallback
, since Base
needs to run disconnectedCallback
as well. It is overridden in each element that needs to run disconnectedCallback
.
Returns
void
Inherited from
BaseCopy.destroy
disconnectedCallback()
disconnectedCallback():
void
Defined in: base/index.ts:181
Called when custom element is removed from the page.
Returns
void
Inherited from
BaseCopy.disconnectedCallback
getContent()
getContent<
T
>(instance
):T
Defined in: base/index.ts:74
Type Parameters
• T extends HTMLElement
= HTMLElement
Parameters
instance
() => T
The instance of the desired element to validate against,
ex: HTMLDialogElement
. Defaults to HTMLElement
.
Returns
T
The element that matches the content
selector.
Default
this.querySelector("[data-content]");
Inherited from
BaseCopy.getContent
getTrigger()
getTrigger<
T
>():NodeListOf
<T
>
Defined in: base/index.ts:61
Type Parameters
• T extends HTMLElement
= HTMLElement
Returns
NodeListOf
<T
>
All of the elements that match the trigger
selector.
Default
this.querySelectorAll("[data-trigger]");
Inherited from
BaseCopy.getTrigger
mount()
mount():
void
Defined in: share/index.ts:34
Passed into queueMicrotask
in connectedCallback
. It is overridden in each component that needs to run connectedCallback
.
The reason for this is to make these elements work better with frameworks like Svelte. For SSR this isn’t necessary, but when client side rendering, the HTML within the custom element isn’t available before connectedCallback
is called. By waiting until the next microtask, the HTML content is available—then for example, listeners can be attached to elements inside.
Returns
void
Overrides
BaseCopy.mount
safeListener()
safeListener<
K
,T
>(type
,listener
,element
,options
):void
Defined in: base/index.ts:136
Wrapper around document.body.addEventListener
that ensures when the
element is removed from the DOM, these event listeners are cleaned up.
Type Parameters
• K extends keyof DocumentEventMap
• T extends HTMLElement
| Document
| Window
= HTMLElement
Parameters
type
K
listener
(this
, ev
) => any
element
T
= ...
options
AddEventListenerOptions
= {}
Returns
void
Inherited from
BaseCopy.safeListener
share()
share(
url
):Promise
<void
>
Defined in: share/index.ts:19
Shares or copies the value
.
Parameters
url
string
= ...
The url
to share, defaults to this.value
Returns
Promise
<void
>
An object containing a result
- whether the url
was copied or shared
depending on browser support.
swapContent()
swapContent(
revert
):void
Defined in: base/index.ts:93
Finds the HTMLElement | HTMLTemplateElement
via the swap
selector and
swaps this.content()
with the content of the element found.
Parameters
revert
Wait time (ms) before swapping back, set to false
to not revert.
default: 800
number
| false
Returns
void
Inherited from
BaseCopy.swapContent
triggerListener()
triggerListener<
T
,K
>(listener
,type
,options
?):void
Defined in: base/index.ts:153
Type Parameters
• T extends HTMLElement
• K extends keyof HTMLElementEventMap
Parameters
listener
(this
, e
) => any
Listener to attach to all of the trigger
elements.
type
K
= ...
options?
AddEventListenerOptions
Returns
void
Inherited from
BaseCopy.triggerListener