forked from tldraw/tldraw
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(poll): Wrap overly long poll options
- Loading branch information
1 parent
eae6fcb
commit f425e30
Showing
2 changed files
with
39 additions
and
1 deletion.
There are no files selected for viewing
37 changes: 37 additions & 0 deletions
37
packages/tldraw/src/lib/shapes/poll/components/CustomizedAxisTick.tsx
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,37 @@ | ||
import React from 'react'; | ||
|
||
function getAverageCharacterWidth(text: string, font: string) { | ||
const canvas = document.createElement('canvas'); | ||
const ctx = canvas.getContext('2d'); | ||
|
||
if (!ctx) return null; | ||
|
||
ctx.font = font; | ||
|
||
const textWidth = ctx.measureText(text).width; | ||
const averageAdvance = textWidth / text.length; | ||
|
||
return averageAdvance; | ||
} | ||
|
||
const TICK_SIZE = 6; | ||
const AVERAGE_CHAR_WIDTH = getAverageCharacterWidth('0', 'Source Sans Pro') ?? 6; | ||
const ELLIPSIS = '...'; | ||
|
||
const CustomizedAxisTick = (props: any) => { | ||
const { payload, ...restProps } = props; | ||
const { width } = restProps; | ||
const numberOfChars = Math.floor((width - TICK_SIZE) / AVERAGE_CHAR_WIDTH); | ||
const restValue = payload.value.substring(numberOfChars, payload.value.length); | ||
|
||
return ( | ||
<g> | ||
<text {...restProps}> | ||
{payload.value.substring(0, restValue.length > 0 ? numberOfChars - ELLIPSIS.length : numberOfChars)} | ||
{restValue.length > 0 && ELLIPSIS} | ||
</text> | ||
</g> | ||
); | ||
}; | ||
|
||
export default CustomizedAxisTick; |
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