Browser Utilities API reference for browser-specific utility functions globe Guide

Browser Utilities

The Browser utilities provide functions for common tasks in browser environments, such as copying text, handling keyboard events, and fetching text content.

Functions

copyText

function copyText(text)

Copies the provided text to the clipboard.

Parameters

NameTypeDescription
textstringThe text to copy to the clipboard

Example

import { copyText } from '@semantic-ui/utils';
copyText('Hello, world!');
console.log('Text copied to clipboard');

Browser Support This function uses the modern navigator.clipboard.writeText API. Ensure compatibility with your target browsers or provide a fallback for older browsers.

getKeyFromEvent

function getKeyFromEvent(event)

Extracts a standardized key representation from a keyboard event.

Parameters

NameTypeDescription
eventKeyboardEventThe keyboard event object

Returns

A string representing the key pressed, including any modifier keys (e.g., “ctrl+a”).

Example

import { getKeyFromEvent } from '@semantic-ui/utils';
document.addEventListener('keydown', (event) => {
const key = getKeyFromEvent(event);
console.log(`Key pressed: ${key}`);
});

Modifier Keys This function handles modifier keys (Ctrl, Alt, Shift, Meta) and combines them with the pressed key. It also normalizes some key names for consistency.

getText

async function getText(src)

Fetches text content from a given URL.

Parameters

NameTypeDescription
srcstringThe URL to fetch the text from

Returns

A Promise that resolves with the fetched text content.

Example

import { defineComponent } from '@semantic-ui/core';
import { getText } from '@semantic-ui/utils';
const template = await getText('/templates/component.html');
const css = await getText('/templates/component.css');
defineComponent({
template,
css
});

Usage Note This can be used on the server to load static assets like templates or css