-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1751 from Shopify/passanelli/grid_responsiveness_…
…improvements feature, Improves Grid chart labels and values responsivenes
- Loading branch information
Showing
5 changed files
with
77 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 23 additions & 0 deletions
23
packages/polaris-viz/src/components/Grid/utilities/truncate-text.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import {estimateStringWidth} from '@shopify/polaris-viz-core'; | ||
|
||
export function truncateText( | ||
text: string, | ||
maxWidth: number, | ||
characterWidths: {[key: string]: number}, | ||
) { | ||
const estimatedWidth = estimateStringWidth(text, characterWidths); | ||
|
||
if (estimatedWidth <= maxWidth) { | ||
return text; | ||
} | ||
|
||
let truncated = text; | ||
while ( | ||
estimateStringWidth(`${truncated}...`, characterWidths) > maxWidth && | ||
truncated.length > 0 | ||
) { | ||
truncated = truncated.slice(0, -1); | ||
} | ||
|
||
return truncated.length === 0 ? '' : `${truncated}...`; | ||
} |