breaking(actions): move truncate action to src/Truncate (#1224)

* breaking: move truncate action to src/Truncate

* docs: update truncate docs
This commit is contained in:
metonym 2022-04-02 12:04:33 -07:00 committed by GitHub
commit 9143e50244
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
10 changed files with 39 additions and 19 deletions

View file

@ -1 +0,0 @@
export { truncate } from "./truncate";

View file

@ -1,7 +1,6 @@
<script> <script>
import { Truncate } from "carbon-components-svelte"; import { Truncate, truncate } from "carbon-components-svelte";
import { truncate } from "carbon-components-svelte/actions";
import Preview from "../../components/Preview.svelte"; import Preview from "../../components/Preview.svelte";
</script> </script>
@ -25,9 +24,7 @@ Set `clamp` to `"front"` to clamp the text from the front.
### use:truncate ### use:truncate
The `truncate` action can be used on other HTML elements. The `truncate` action can be used on plain HTML elements.
Import path: `import { truncate } from "carbon-components-svelte/actions";`
<h4 use:truncate> <h4 use:truncate>
Carbon Components Svelte is a Svelte component library that implements the Carbon Design System, an open source design system by IBM. Carbon Components Svelte is a Svelte component library that implements the Carbon Design System, an open source design system by IBM.

View file

@ -75,8 +75,7 @@
"lib", "lib",
"src", "src",
"types", "types",
"css", "css"
"actions"
], ],
"contributors": [ "contributors": [
"Eric Liu (https://github.com/metonym)", "Eric Liu (https://github.com/metonym)",

View file

@ -1 +1,2 @@
export { default as Truncate } from "./Truncate.svelte"; export { default as Truncate } from "./Truncate.svelte";
export { truncate } from "./truncate";

12
src/Truncate/truncate.d.ts vendored Normal file
View file

@ -0,0 +1,12 @@
interface TruncateOptions {
clamp?: "end" | "front";
}
export function TruncateAction(
node: HTMLElement,
options?: TruncateOptions
): {
update: (options?: TruncateOptions) => void;
};
export default TruncateAction;

View file

@ -1,16 +1,12 @@
/** /**
* Svelte action that applies single-line text truncation to an element * Svelte action that applies single-line text truncation to an element
* @param {HTMLElement} node * @typedef {{ clamp?: "end" | "front" }} TruncateOptions
* @param {{ clamp?: "end" | "front" }} params * @type {(node: HTMLElement, options?: TruncateOptions) => { update: (options?: TruncateOptions) => void; }}
* @example * @example
* <script>
* import { truncate } from "carbon-components-svelte/actions";
* </script>
*
* <h1 use:truncate>...</h1> * <h1 use:truncate>...</h1>
* <h1 use:truncate={{ clamp: "front" }}>...</h1> * <h1 use:truncate={{ clamp: "front" }}>...</h1>
*/ */
export function truncate(node, params = {}) { export function truncate(node, options = {}) {
const prefix = "bx--text-truncate--"; const prefix = "bx--text-truncate--";
function toggleClass(front = false) { function toggleClass(front = false) {
@ -21,11 +17,13 @@ export function truncate(node, params = {}) {
node.className = `${classes} ${prefix}${front ? "front" : "end"}`; node.className = `${classes} ${prefix}${front ? "front" : "end"}`;
} }
toggleClass(params.clamp === "front"); toggleClass(options.clamp === "front");
return { return {
update(params) { update(options) {
toggleClass(params.clamp === "front"); toggleClass(options.clamp === "front");
}, },
}; };
} }
export default truncate;

View file

@ -128,6 +128,7 @@ export { TooltipDefinition } from "./TooltipDefinition";
export { TooltipIcon } from "./TooltipIcon"; export { TooltipIcon } from "./TooltipIcon";
export { TreeView } from "./TreeView"; export { TreeView } from "./TreeView";
export { Truncate } from "./Truncate"; export { Truncate } from "./Truncate";
export { default as truncate } from "./Truncate/truncate";
export { export {
Header, Header,
HeaderAction, HeaderAction,

View file

@ -1,6 +1,6 @@
<script lang="ts"> <script lang="ts">
import { Truncate } from "../types"; import { Truncate } from "../types";
import { truncate } from "../actions"; import { truncate } from "../types";
</script> </script>
<Truncate> <Truncate>

12
types/Truncate/truncate.d.ts vendored Normal file
View file

@ -0,0 +1,12 @@
interface TruncateOptions {
clamp?: "end" | "front";
}
export function TruncateAction(
node: HTMLElement,
options?: TruncateOptions
): {
update: (options?: TruncateOptions) => void;
};
export default TruncateAction;

1
types/index.d.ts vendored
View file

@ -144,6 +144,7 @@ export { default as TooltipDefinition } from "./TooltipDefinition/TooltipDefinit
export { default as TooltipIcon } from "./TooltipIcon/TooltipIcon.svelte"; export { default as TooltipIcon } from "./TooltipIcon/TooltipIcon.svelte";
export { default as TreeView } from "./TreeView/TreeView.svelte"; export { default as TreeView } from "./TreeView/TreeView.svelte";
export { default as Truncate } from "./Truncate/Truncate.svelte"; export { default as Truncate } from "./Truncate/Truncate.svelte";
export { default as truncate } from "./Truncate/truncate";
export { default as Header } from "./UIShell/Header.svelte"; export { default as Header } from "./UIShell/Header.svelte";
export { default as HeaderAction } from "./UIShell/HeaderAction.svelte"; export { default as HeaderAction } from "./UIShell/HeaderAction.svelte";
export { default as HeaderActionLink } from "./UIShell/HeaderActionLink.svelte"; export { default as HeaderActionLink } from "./UIShell/HeaderActionLink.svelte";