Skip to content

Commit

Permalink
breaking(actions): move truncate action to src/Truncate (carbon-des…
Browse files Browse the repository at this point in the history
…ign-system#1224)

* breaking: move truncate action to src/Truncate

* docs: update truncate docs
  • Loading branch information
metonym authored Apr 2, 2022
1 parent 62735d6 commit 9143e50
Show file tree
Hide file tree
Showing 10 changed files with 39 additions and 19 deletions.
1 change: 0 additions & 1 deletion actions/index.js

This file was deleted.

7 changes: 2 additions & 5 deletions docs/src/pages/components/Truncate.svx
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@

<script>
import { Truncate } from "carbon-components-svelte";
import { truncate } from "carbon-components-svelte/actions";
import { Truncate, truncate } from "carbon-components-svelte";
import Preview from "../../components/Preview.svelte";
</script>

Expand All @@ -25,9 +24,7 @@ Set `clamp` to `"front"` to clamp the text from the front.

### use:truncate

The `truncate` action can be used on other HTML elements.

Import path: `import { truncate } from "carbon-components-svelte/actions";`
The `truncate` action can be used on plain HTML elements.

<h4 use:truncate>
Carbon Components Svelte is a Svelte component library that implements the Carbon Design System, an open source design system by IBM.
Expand Down
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,7 @@
"lib",
"src",
"types",
"css",
"actions"
"css"
],
"contributors": [
"Eric Liu (https://github.com/metonym)",
Expand Down
1 change: 1 addition & 0 deletions src/Truncate/index.js
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export { default as Truncate } from "./Truncate.svelte";
export { truncate } from "./truncate";
12 changes: 12 additions & 0 deletions src/Truncate/truncate.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
interface TruncateOptions {
clamp?: "end" | "front";
}

export function TruncateAction(
node: HTMLElement,
options?: TruncateOptions
): {
update: (options?: TruncateOptions) => void;
};

export default TruncateAction;
18 changes: 8 additions & 10 deletions actions/truncate.js → src/Truncate/truncate.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,12 @@
/**
* Svelte action that applies single-line text truncation to an element
* @param {HTMLElement} node
* @param {{ clamp?: "end" | "front" }} params
* @typedef {{ clamp?: "end" | "front" }} TruncateOptions
* @type {(node: HTMLElement, options?: TruncateOptions) => { update: (options?: TruncateOptions) => void; }}
* @example
* <script>
* import { truncate } from "carbon-components-svelte/actions";
* </script>
*
* <h1 use:truncate>...</h1>
* <h1 use:truncate={{ clamp: "front" }}>...</h1>
*/
export function truncate(node, params = {}) {
export function truncate(node, options = {}) {
const prefix = "bx--text-truncate--";

function toggleClass(front = false) {
Expand All @@ -21,11 +17,13 @@ export function truncate(node, params = {}) {
node.className = `${classes} ${prefix}${front ? "front" : "end"}`;
}

toggleClass(params.clamp === "front");
toggleClass(options.clamp === "front");

return {
update(params) {
toggleClass(params.clamp === "front");
update(options) {
toggleClass(options.clamp === "front");
},
};
}

export default truncate;
1 change: 1 addition & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ export { TooltipDefinition } from "./TooltipDefinition";
export { TooltipIcon } from "./TooltipIcon";
export { TreeView } from "./TreeView";
export { Truncate } from "./Truncate";
export { default as truncate } from "./Truncate/truncate";
export {
Header,
HeaderAction,
Expand Down
2 changes: 1 addition & 1 deletion tests/Truncate.test.svelte
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<script lang="ts">
import { Truncate } from "../types";
import { truncate } from "../actions";
import { truncate } from "../types";
</script>

<Truncate>
Expand Down
12 changes: 12 additions & 0 deletions types/Truncate/truncate.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
interface TruncateOptions {
clamp?: "end" | "front";
}

export function TruncateAction(
node: HTMLElement,
options?: TruncateOptions
): {
update: (options?: TruncateOptions) => void;
};

export default TruncateAction;
1 change: 1 addition & 0 deletions types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ export { default as TooltipDefinition } from "./TooltipDefinition/TooltipDefinit
export { default as TooltipIcon } from "./TooltipIcon/TooltipIcon.svelte";
export { default as TreeView } from "./TreeView/TreeView.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 HeaderAction } from "./UIShell/HeaderAction.svelte";
export { default as HeaderActionLink } from "./UIShell/HeaderActionLink.svelte";
Expand Down

0 comments on commit 9143e50

Please sign in to comment.