@charcoal-ui/react 5.3.0-beta.2 → 5.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,50 @@
1
+ // https://github.com/fernandopasik/react-children-utilities/blob/971d8a0324e6183734d8d1af9a65dbad18ab3d00/src/lib/onlyText.ts
2
+
3
+ import { Children, isValidElement, ReactElement, ReactNode } from 'react'
4
+
5
+ const hasChildren = (
6
+ element: ReactNode,
7
+ ): element is ReactElement<{ children: ReactNode[] }> =>
8
+ isValidElement<{ children?: ReactNode[] }>(element) &&
9
+ Boolean(element.props.children)
10
+
11
+ export const childToString = (
12
+ child?: number | string | boolean | {} | null,
13
+ ): string => {
14
+ if (
15
+ typeof child === 'undefined' ||
16
+ child === null ||
17
+ typeof child === 'boolean'
18
+ ) {
19
+ return ''
20
+ }
21
+
22
+ if (JSON.stringify(child) === '{}') {
23
+ return ''
24
+ }
25
+
26
+ return (child as string | number).toString()
27
+ }
28
+
29
+ export const onlyText = (children: ReactNode): string => {
30
+ if (!Array.isArray(children) && !isValidElement(children)) {
31
+ return childToString(children)
32
+ }
33
+
34
+ return Children.toArray(children).reduce(
35
+ (text: string, child: ReactNode): string => {
36
+ let newText = ''
37
+
38
+ if (isValidElement(child) && hasChildren(child)) {
39
+ newText = onlyText(child.props.children)
40
+ } else if (isValidElement(child) && !hasChildren(child)) {
41
+ newText = ''
42
+ } else {
43
+ newText = childToString(child)
44
+ }
45
+
46
+ return text.concat(newText)
47
+ },
48
+ '',
49
+ )
50
+ }
@@ -0,0 +1,20 @@
1
+ .charcoal-text-ellipsis {
2
+ overflow: hidden;
3
+ line-height: var(--charcoal-text-ellipsis-line-height);
4
+ overflow-wrap: break-word;
5
+ }
6
+
7
+ .charcoal-text-ellipsis[data-line-limit='1'] {
8
+ text-overflow: ellipsis;
9
+ white-space: nowrap;
10
+ }
11
+
12
+ .charcoal-text-ellipsis[data-line-limit]:not([data-line-limit='1']) {
13
+ display: -webkit-box;
14
+ -webkit-box-orient: vertical;
15
+ -webkit-line-clamp: var(--charcoal-text-ellipsis-line-limit);
16
+ max-height: calc(
17
+ var(--charcoal-text-ellipsis-line-height) *
18
+ var(--charcoal-text-ellipsis-line-limit)
19
+ );
20
+ }
@@ -0,0 +1,43 @@
1
+ import './index.css'
2
+
3
+ import { onlyText } from './helper'
4
+ import { useClassNames } from '../../_lib/useClassNames'
5
+
6
+ export type TextEllipsisProps = {
7
+ lineHeight: number
8
+ lineLimit?: number
9
+ } & React.ComponentPropsWithoutRef<'div'>
10
+
11
+ /**
12
+ * 複数行のテキストに表示行数制限を設けて`...`で省略する
13
+ */
14
+ export default function TextEllipsis({
15
+ lineHeight,
16
+ lineLimit = 1,
17
+ children,
18
+ title,
19
+ ...props
20
+ }: TextEllipsisProps) {
21
+ const resolvedTitle = title === undefined ? onlyText(children) : title
22
+ const finalTitle = resolvedTitle !== '' ? resolvedTitle : undefined
23
+
24
+ const classNames = useClassNames('charcoal-text-ellipsis', props.className)
25
+
26
+ return (
27
+ <div
28
+ {...props}
29
+ className={classNames}
30
+ data-line-limit={lineLimit}
31
+ style={
32
+ {
33
+ '--charcoal-text-ellipsis-line-height': `${lineHeight}px`,
34
+ '--charcoal-text-ellipsis-line-limit': lineLimit,
35
+ ...props.style,
36
+ } satisfies React.CSSProperties
37
+ }
38
+ title={finalTitle}
39
+ >
40
+ {children}
41
+ </div>
42
+ )
43
+ }
package/src/index.ts CHANGED
@@ -79,3 +79,7 @@ export {
79
79
  type HintTextProps,
80
80
  type HintTextContext,
81
81
  } from './components/HintText'
82
+ export {
83
+ default as UnstableTextEllipsis,
84
+ type TextEllipsisProps,
85
+ } from './components/TextEllipsis'