Skip to content

Commit

Permalink
fix: [Notice] update the logic of checking the number of lines
Browse files Browse the repository at this point in the history
  • Loading branch information
akai committed May 14, 2021
1 parent 4096ee3 commit 1fcd117
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 17 deletions.
9 changes: 5 additions & 4 deletions src/components/Notice/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import React, { useState, useEffect, useRef } from 'react';
import clsx from 'clsx';
import { Icon } from '../Icon';
import { IconButton } from '../IconButton';
import countLines from '../../utils/countLines';

export interface NoticeProps {
content: string;
Expand All @@ -11,8 +12,6 @@ export interface NoticeProps {
onLinkClick?: (url: string) => void;
}

const COLLAPSED_MAX_HEIGHT = 48;

export const Notice: React.FC<NoticeProps> = (props) => {
const { content, url, hasClose = true, onLinkClick, onClose } = props;
// 展开还是收起状态
Expand All @@ -34,7 +33,8 @@ export const Notice: React.FC<NoticeProps> = (props) => {
}

useEffect(() => {
if (contentRef.current && contentRef.current.offsetHeight > COLLAPSED_MAX_HEIGHT) {
const cont = contentRef.current;
if (cont && countLines(cont) > 2) {
setHasMore(true);
setCollapsed(true);
}
Expand All @@ -52,7 +52,7 @@ export const Notice: React.FC<NoticeProps> = (props) => {
/>
)}
<div className="Notice-content">
<p className={clsx('Notice-text', { collapsed })} ref={contentRef}>
<p className={clsx('Notice-text', { collapsed })} data-overflow={hasMore} ref={contentRef}>
<Icon className="Notice-icon" type="bullhorn" />
{url ? (
<a href={url} onClick={handleLinkClick}>
Expand All @@ -70,6 +70,7 @@ export const Notice: React.FC<NoticeProps> = (props) => {
size="lg"
aria-expanded={!collapsed}
onClick={handleToggle}
aria-label="Toggle Notice"
/>
</div>
)}
Expand Down
18 changes: 5 additions & 13 deletions src/components/Notice/style.less
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
.Notice {
@icon-size: @font-size-root * 1.5;

position: absolute;
z-index: @zindex-notice;
top: @notice-top;
Expand All @@ -20,10 +22,10 @@
-webkit-line-clamp: 2;
text-overflow: ellipsis;
}
&:not(.collapsed)::after {
&[data-overflow="true"]:not(.collapsed)::after {
content: '';
display: inline-block;
width: (@notice-more-font-size + 6px);
width: (@icon-size + 6px);
height: @notice-content-font-size;
}
a {
Expand All @@ -42,10 +44,9 @@
top: 50%;
top: ~'min(50%, 33px)';
transform: translateY(-50%);
font-size: @notice-close-font-size;

& + .Notice-content {
margin-left: (@notice-close-font-size + @gutter - @icon-button-lg-padding);
margin-left: (@icon-size + $left);
}
}
.Icon {
Expand All @@ -70,16 +71,7 @@
}

.Notice-more {
display: block;
color: @notice-more-color;
font-size: @notice-more-font-size;
transition: 0.3s;

&[aria-expanded='true'] {
transform: rotate(180deg);
}
&:hover,
&:active {
background: transparent;
}
}
6 changes: 6 additions & 0 deletions src/utils/countLines.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export default function countLines(el: Element) {
const styles = window.getComputedStyle(el, null);
const lh = parseInt(styles.lineHeight, 10);
const h = parseInt(styles.height, 10);
return Math.round(h / lh);
}

0 comments on commit 1fcd117

Please sign in to comment.