feat(truncate): add text truncation component and action

This commit is contained in:
Eric Liu 2021-02-19 07:40:19 -08:00
commit a51c300a79
13 changed files with 161 additions and 3 deletions

24
actions/truncate.js Normal file
View file

@ -0,0 +1,24 @@
/**
* Svelte action that applies single-line text truncation to an element
* @param {HTMLElement} node
* @param {{ direction?: "end" | "front" }} params
* @example
* <script>
* import { truncate } from "carbon-components-svelte/actions";
* </script>
*
* <h1 use:truncate>...</h1>
*/
export function truncate(node, params = {}) {
function toggleClass(front = false) {
node.className = `bx--text-truncate--${front ? "front" : "end"}`;
}
toggleClass(params.direction === "front");
return {
update(params) {
toggleClass(params.direction === "front");
},
};
}