mirror of
https://github.com/carbon-design-system/carbon-components-svelte.git
synced 2025-09-18 11:36:36 +00:00
31 lines
790 B
JavaScript
31 lines
790 B
JavaScript
// @ts-check
|
|
|
|
/**
|
|
* Svelte action that applies single-line text truncation to an element
|
|
* @typedef {{ clamp?: "end" | "front" }} TruncateOptions
|
|
* @type {import ("svelte/action").Action<HTMLElement, TruncateOptions>}
|
|
* @example
|
|
* <h1 use:truncate>...</h1>
|
|
* <h1 use:truncate={{ clamp: "front" }}>...</h1>
|
|
*/
|
|
export function truncate(node, options = {}) {
|
|
const prefix = "bx--text-truncate-";
|
|
|
|
function toggleClass(front = false) {
|
|
const classes = [...node.classList]
|
|
.filter((name) => !name.startsWith(prefix))
|
|
.join(" ");
|
|
|
|
node.className = `${classes} ${prefix}${front ? "front" : "end"}`;
|
|
}
|
|
|
|
toggleClass(options.clamp === "front");
|
|
|
|
return {
|
|
update(options) {
|
|
toggleClass(options.clamp === "front");
|
|
},
|
|
};
|
|
}
|
|
|
|
export default truncate;
|