Skip to content

Commit 16d53b4

Browse files
committed
docs: use css for truncation instead of the TruncateText in ColorCards
1 parent 93d921b commit 16d53b4

3 files changed

Lines changed: 79 additions & 8 deletions

File tree

packages/__docs__/src/ColorName/index.tsx

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,17 @@
2525
import { Component } from 'react'
2626

2727
import { Text } from '@instructure/ui-text'
28-
import { TruncateText } from '@instructure/ui-truncate-text'
2928
import { Tooltip } from '@instructure/ui-tooltip'
3029
import { ScreenReaderContent } from '@instructure/ui-a11y-content'
31-
import type { ColorNameProps, ColorNameState } from './props'
30+
31+
import { withStyleForDocs } from '../withStyleForDocs'
32+
import generateStyle from './styles'
33+
3234
import { allowedProps } from './props'
35+
import type { ColorNameProps, ColorNameState } from './props'
36+
37+
// CSS truncation instead of heavy TruncateText
38+
@withStyleForDocs(generateStyle, () => ({}))
3339
class ColorName extends Component<ColorNameProps, ColorNameState> {
3440
static displayName = 'ColorName'
3541
static allowedProps = allowedProps
@@ -41,18 +47,38 @@ class ColorName extends Component<ColorNameProps, ColorNameState> {
4147
isTruncated: false
4248
}
4349

44-
handleUpdate = (isTruncated: boolean) => {
50+
private strongRef: HTMLElement | null = null
51+
52+
componentDidMount() {
53+
this.props.makeStyles?.()
54+
this.checkTruncation()
55+
}
56+
57+
componentDidUpdate(prevProps: ColorNameProps) {
58+
this.props.makeStyles?.()
59+
if (prevProps.name !== this.props.name) {
60+
this.checkTruncation()
61+
}
62+
}
63+
64+
checkTruncation = () => {
65+
if (!this.strongRef) return
66+
const isTruncated = this.strongRef.scrollWidth > this.strongRef.clientWidth
4567
if (this.state.isTruncated !== isTruncated) {
4668
this.setState({ isTruncated })
4769
}
4870
}
4971

72+
handleStrongRef = (el: HTMLElement | null) => {
73+
this.strongRef = el
74+
}
75+
5076
renderText() {
51-
const { name, ...passthrough } = this.props
77+
const { name, styles, makeStyles, ...passthrough } = this.props
5278
return (
5379
<Text {...passthrough}>
54-
<strong aria-hidden>
55-
<TruncateText onUpdate={this.handleUpdate}>{name}</TruncateText>
80+
<strong aria-hidden ref={this.handleStrongRef} css={styles?.truncate}>
81+
{name}
5682
</strong>
5783
<ScreenReaderContent>{name}</ScreenReaderContent>
5884
</Text>

packages/__docs__/src/ColorName/props.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,18 +23,21 @@
2323
*/
2424

2525
import type { AsElementType } from '@instructure/shared-types'
26+
import type { ComponentStyle, WithStyleProps } from '@instructure/emotion'
27+
2628
type ColorNameOwnProps = {
2729
name: string
2830
as?: AsElementType
2931
lineHeight?: 'default' | 'fit' | 'condensed' | 'double'
3032
}
3133
type PropKeys = keyof ColorNameOwnProps
3234
type AllowedPropKeys = Readonly<Array<PropKeys>>
33-
type ColorNameProps = ColorNameOwnProps
35+
type ColorNameProps = ColorNameOwnProps & WithStyleProps<null, ColorNameStyle>
3436

3537
const allowedProps: AllowedPropKeys = ['name', 'as', 'lineHeight']
3638
type ColorNameState = {
3739
isTruncated: boolean
3840
}
41+
type ColorNameStyle = ComponentStyle<'truncate'>
3942
export { allowedProps }
40-
export type { ColorNameState, ColorNameProps }
43+
export type { ColorNameState, ColorNameProps, ColorNameStyle }
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
* The MIT License (MIT)
3+
*
4+
* Copyright (c) 2015 - present Instructure, Inc.
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in all
14+
* copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
* SOFTWARE.
23+
*/
24+
import type { ColorNameStyle } from './props'
25+
26+
/**
27+
* Generates the style object from the theme and provided additional information
28+
* @return {Object} The final style object, which will be used in the component
29+
*/
30+
const generateStyle = (): ColorNameStyle => {
31+
return {
32+
truncate: {
33+
label: 'colorName__truncate',
34+
display: 'block',
35+
overflow: 'hidden',
36+
textOverflow: 'ellipsis',
37+
whiteSpace: 'nowrap'
38+
}
39+
}
40+
}
41+
42+
export default generateStyle

0 commit comments

Comments
 (0)