idea-react 2.0.0 → 2.2.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,23 @@
1
+ import { FC, isValidElement } from 'react';
2
+ import reactElementToJSXString from 'react-element-to-jsx-string';
3
+ import { LiveEditor, LiveError, LivePreview, LiveProvider } from 'react-live';
4
+
5
+ import 'prismjs/components/prism-javascript';
6
+ import 'prismjs/components/prism-jsx';
7
+ import 'prismjs/components/prism-tsx';
8
+ import 'prismjs/components/prism-typescript';
9
+
10
+ export type LiveTSXProps = Pick<Parameters<typeof LiveProvider>[0], 'scope' | 'children'>;
11
+
12
+ export const LiveTSX: FC<LiveTSXProps> = ({ scope, children }) => {
13
+ const element = isValidElement(children) ? children : <>{children}</>;
14
+ const code = typeof children === 'string' ? children : reactElementToJSXString(element);
15
+
16
+ return (
17
+ <LiveProvider code={code} scope={scope} language="tsx">
18
+ <LivePreview className="mb-3" />
19
+ <LiveEditor />
20
+ <LiveError className="text-danger small p-2" />
21
+ </LiveProvider>
22
+ );
23
+ };
@@ -0,0 +1,16 @@
1
+ .scrollerInner {
2
+ -webkit-overflow-scrolling: touch;
3
+ scrollbar-width: none;
4
+
5
+ &::-webkit-scrollbar {
6
+ display: none;
7
+ }
8
+ }
9
+
10
+ .scrollButtonLeft {
11
+ background: linear-gradient(to right, lightgray, transparent);
12
+ }
13
+
14
+ .scrollButtonRight {
15
+ background: linear-gradient(to left, lightgray, transparent);
16
+ }
@@ -0,0 +1,109 @@
1
+ import { computed, observable } from 'mobx';
2
+ import { observer } from 'mobx-react';
3
+ import { Component, createRef } from 'react';
4
+ import { Nav, NavProps } from 'react-bootstrap';
5
+
6
+ import { Icon } from '../Icon';
7
+ import * as style from './index.module.less';
8
+
9
+ export type ScrollNavProps = NavProps;
10
+
11
+ /**
12
+ * A horizontal scrolling navigation bar based on Bootstrap's `nav-scroller` pattern.
13
+ * Automatically shows scroll arrow buttons when nav items overflow the container width.
14
+ *
15
+ * @see {@link https://github.com/twbs/bootstrap/blob/main/site/src/assets/examples/offcanvas-navbar/index.astro#L50-L65}
16
+ */
17
+ @observer
18
+ export class ScrollNav extends Component<ScrollNavProps> {
19
+ static displayName = 'ScrollNav';
20
+
21
+ scrollerRef = createRef<HTMLDivElement>();
22
+
23
+ @observable
24
+ accessor scrollLeft = 0;
25
+
26
+ @observable
27
+ accessor scrollWidth = 0;
28
+
29
+ @observable
30
+ accessor clientWidth = 0;
31
+
32
+ @computed
33
+ get canScrollLeft() {
34
+ return this.scrollLeft > 0;
35
+ }
36
+
37
+ @computed
38
+ get canScrollRight() {
39
+ return this.scrollLeft + this.clientWidth < this.scrollWidth;
40
+ }
41
+
42
+ updateScroll = () => {
43
+ const scroller = this.scrollerRef.current;
44
+ if (scroller) {
45
+ this.scrollLeft = scroller.scrollLeft;
46
+ this.scrollWidth = scroller.scrollWidth;
47
+ this.clientWidth = scroller.clientWidth;
48
+ }
49
+ return scroller;
50
+ };
51
+
52
+ private resizeObserver?: ResizeObserver;
53
+
54
+ componentDidMount() {
55
+ const scroller = this.updateScroll();
56
+ if (!scroller) return;
57
+
58
+ scroller.addEventListener('scroll', this.updateScroll);
59
+ this.resizeObserver = new ResizeObserver(this.updateScroll);
60
+ this.resizeObserver.observe(scroller);
61
+ }
62
+
63
+ componentWillUnmount() {
64
+ this.resizeObserver?.disconnect();
65
+ this.scrollerRef.current?.removeEventListener('scroll', this.updateScroll);
66
+ }
67
+
68
+ scrollBy = (direction: -1 | 1) => () => {
69
+ const scroller = this.scrollerRef.current;
70
+ if (!scroller) return;
71
+
72
+ scroller.scrollBy({ left: direction * scroller.clientWidth * 0.8, behavior: 'smooth' });
73
+ };
74
+
75
+ render() {
76
+ const { canScrollLeft, canScrollRight } = this;
77
+ const { className = '', children, ...props } = this.props;
78
+
79
+ return (
80
+ <div className={`position-relative overflow-y-hidden ${className}`}>
81
+ {canScrollLeft && (
82
+ <button
83
+ type="button"
84
+ className={`position-absolute top-0 bottom-0 start-0 z-1 border-0 px-2 ${style.scrollButtonLeft}`}
85
+ aria-label="Scroll left"
86
+ onClick={this.scrollBy(-1)}
87
+ >
88
+ <Icon name="caret-left-fill" />
89
+ </button>
90
+ )}
91
+ <div ref={this.scrollerRef} className={`overflow-x-auto ${style.scrollerInner}`}>
92
+ <Nav {...props} className="flex-nowrap">
93
+ {children}
94
+ </Nav>
95
+ </div>
96
+ {canScrollRight && (
97
+ <button
98
+ type="button"
99
+ className={`position-absolute top-0 bottom-0 end-0 z-1 border-0 px-2 ${style.scrollButtonRight}`}
100
+ aria-label="Scroll right"
101
+ onClick={this.scrollBy(1)}
102
+ >
103
+ <Icon name="caret-right-fill" />
104
+ </button>
105
+ )}
106
+ </div>
107
+ );
108
+ }
109
+ }
@@ -0,0 +1,76 @@
1
+ import { observable } from 'mobx';
2
+ import { observer } from 'mobx-react';
3
+ import { Component, createRef, CSSProperties, HTMLAttributes, ReactNode } from 'react';
4
+
5
+ export interface TextTruncateProps extends HTMLAttributes<HTMLDivElement> {
6
+ rows?: number;
7
+ expandText?: ReactNode;
8
+ collapseText?: ReactNode;
9
+ }
10
+
11
+ @observer
12
+ export class TextTruncate extends Component<TextTruncateProps> {
13
+ static displayName = 'TextTruncate';
14
+
15
+ @observable
16
+ accessor expanded = false;
17
+
18
+ @observable
19
+ accessor overflowed = false;
20
+
21
+ contentRef = createRef<HTMLParagraphElement>();
22
+
23
+ componentDidMount() {
24
+ this.checkOverflow();
25
+ window.addEventListener('resize', this.checkOverflow);
26
+ }
27
+
28
+ componentDidUpdate({ rows, children }: TextTruncateProps) {
29
+ if (!this.expanded && (children !== this.props.children || rows !== this.props.rows))
30
+ this.checkOverflow();
31
+ }
32
+
33
+ componentWillUnmount() {
34
+ window.removeEventListener('resize', this.checkOverflow);
35
+ }
36
+
37
+ checkOverflow = () => {
38
+ const { current } = this.contentRef;
39
+
40
+ if (current) this.overflowed = current.scrollHeight > current.clientHeight;
41
+ };
42
+
43
+ toggle = () => {
44
+ this.expanded = !this.expanded;
45
+
46
+ if (!this.expanded) requestAnimationFrame(this.checkOverflow);
47
+ };
48
+
49
+ render() {
50
+ const { children, rows = 3, expandText = '⏬', collapseText = '⏫', ...props } = this.props;
51
+ const { expanded, overflowed } = this;
52
+ const style: CSSProperties = {
53
+ overflow: 'hidden',
54
+ display: '-webkit-box',
55
+ WebkitBoxOrient: 'vertical',
56
+ WebkitLineClamp: rows
57
+ };
58
+ return (
59
+ <div {...props}>
60
+ <p ref={this.contentRef} style={expanded ? undefined : style}>
61
+ {children}
62
+ </p>
63
+ {(overflowed || expanded) && (
64
+ <button
65
+ type="button"
66
+ className="bg-transparent border-0 p-0"
67
+ aria-expanded={expanded}
68
+ onClick={this.toggle}
69
+ >
70
+ {expanded ? collapseText : expandText}
71
+ </button>
72
+ )}
73
+ </div>
74
+ );
75
+ }
76
+ }
package/source/Time.tsx CHANGED
@@ -1,4 +1,7 @@
1
- import { FC, TimeHTMLAttributes } from 'react';
1
+ import { observable } from 'mobx';
2
+ import { observer } from 'mobx-react';
3
+ import { ObservedComponent, reaction } from 'mobx-react-helper';
4
+ import { TimeHTMLAttributes } from 'react';
2
5
  import { formatDate, TimeData } from 'web-utility';
3
6
 
4
7
  export interface TimeProps extends Omit<TimeHTMLAttributes<HTMLTimeElement>, 'dateTime'> {
@@ -6,10 +9,33 @@ export interface TimeProps extends Omit<TimeHTMLAttributes<HTMLTimeElement>, 'da
6
9
  format?: string;
7
10
  }
8
11
 
9
- export const Time: FC<TimeProps> = ({ dateTime, format, ...props }) => (
10
- <time dateTime={new Date(dateTime).toJSON()} {...props}>
11
- {formatDate(dateTime, format)}
12
- </time>
13
- );
12
+ @observer
13
+ export class Time extends ObservedComponent<TimeProps> {
14
+ static displayName = 'Time';
14
15
 
15
- Time.displayName = 'Time';
16
+ @reaction(({ observedProps: { dateTime, format } }) => dateTime + format)
17
+ updateText() {
18
+ const { dateTime, format } = this.props;
19
+
20
+ return formatDate(dateTime, format);
21
+ }
22
+
23
+ @observable
24
+ accessor timeText = this.updateText();
25
+
26
+ componentDidMount() {
27
+ super.componentDidMount();
28
+
29
+ this.timeText = this.updateText();
30
+ }
31
+
32
+ render() {
33
+ const { dateTime, ...props } = this.props;
34
+
35
+ return (
36
+ <time dateTime={new Date(dateTime).toJSON()} {...props}>
37
+ {this.timeText}
38
+ </time>
39
+ );
40
+ }
41
+ }
@@ -0,0 +1,51 @@
1
+ import { FC, HTMLAttributes, ReactNode } from 'react';
2
+
3
+ export const ZodiacSigns = ['🐵', '🐔', '🐶', '🐷', '🐭', '🐮', '🐯', '🐰', '🐲', '🐍', '🐴', '🐐'];
4
+
5
+ export const zodiacOf = (year: number) => {
6
+ // Historical year numbering has no year 0: 1 BCE is represented as year -1.
7
+ const astronomicalYear = year < 0 ? year + 1 : year;
8
+
9
+ return ZodiacSigns[((astronomicalYear % 12) + 12) % 12];
10
+ };
11
+
12
+ export interface ZodiacBarProps extends HTMLAttributes<HTMLOListElement> {
13
+ itemClassName?: string;
14
+ startYear: number;
15
+ endYear?: number;
16
+ itemOf?: (year: number, zodiac: string) => { link?: string; title?: ReactNode };
17
+ }
18
+
19
+ export const ZodiacBar: FC<ZodiacBarProps> = ({
20
+ className = '',
21
+ itemClassName = 'border rounded px-4 py-3 d-flex flex-column justify-content-center align-items-center',
22
+ startYear,
23
+ endYear = new Date().getFullYear(),
24
+ itemOf,
25
+ ...props
26
+ }) => (
27
+ <ol
28
+ className={`list-unstyled m-0 d-flex flex-wrap justify-content-center gap-3 ${className}`}
29
+ {...props}
30
+ >
31
+ {Array.from({ length: endYear - startYear + 1 }, (_, index) => {
32
+ const year = endYear - index;
33
+
34
+ if (year === 0) return null;
35
+
36
+ const zodiac = zodiacOf(year);
37
+ const { link = '#', title } = itemOf?.(year, zodiac) || {};
38
+
39
+ return (
40
+ <li key={year} className={itemClassName}>
41
+ <a className="d-inline-block p-3 text-decoration-none text-center" href={link}>
42
+ <div className="fs-1">{zodiac}</div>
43
+
44
+ {title}
45
+ </a>
46
+ </li>
47
+ );
48
+ })}
49
+ </ol>
50
+ );
51
+ ZodiacBar.displayName = 'ZodiacBar';
package/source/index.ts CHANGED
@@ -3,23 +3,28 @@ export * from './Avatar';
3
3
  export * from './ClickBoundary';
4
4
  export * from './CodeBlock';
5
5
  export * from './color';
6
+ export * from './Countdown';
6
7
  export * from './Dialog';
7
8
  export * from './Editor';
8
9
  export * from './EditorHTML';
9
10
  export * from './HorizontalMarquee';
10
11
  export * from './Icon';
12
+ export * from './LiveTSX';
11
13
  export * from './Loading';
12
14
  export * from './MonthCalendar';
13
15
  export * from './Nameplate';
14
16
  export * from './OverlayBox';
15
17
  export * from './PageNav';
18
+ export * from './ScrollNav';
16
19
  export * from './Select';
17
20
  export * from './ShareBox';
18
21
  export * from './SpinnerButton';
19
22
  export * from './TableSpinner';
23
+ export * from './TextTruncate';
20
24
  export * from './Time';
21
25
  export * from './TimeDistance';
22
26
  export * from './Timeline';
23
27
  export * from './TypeEcho';
24
28
  export * from './UserRank';
25
29
  export * from './VerticalMarquee';
30
+ export * from './ZodiacBar';