@transferwise/components 0.0.0-experimental-492a9d3 → 0.0.0-experimental-44a3fcb

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,138 @@
1
+ import classNames from 'classnames';
2
+ import { createRef, useEffect, useRef, useState } from 'react';
3
+
4
+ type SegmentedControlPropsBase = {
5
+ name: string;
6
+ defaultValue: string;
7
+ type: 'input' | 'navigation';
8
+ segments: { label: string; value: string }[];
9
+ onChange: (value: string) => void;
10
+ };
11
+
12
+ type SegmentedControlNavigationProps = {
13
+ type: 'navigation';
14
+ controls: string;
15
+ };
16
+
17
+ type SegmentedControlInputProps = {
18
+ type: 'input';
19
+ controls?: never;
20
+ };
21
+
22
+ export type SegmentedControlProps = SegmentedControlPropsBase &
23
+ (SegmentedControlNavigationProps | SegmentedControlInputProps);
24
+
25
+ export const SegmentedControl = ({
26
+ name,
27
+ defaultValue,
28
+ type,
29
+ segments,
30
+ controls,
31
+ onChange,
32
+ }: SegmentedControlProps) => {
33
+ const [selectedValue, setSelectedValue] = useState(defaultValue || segments[0].value);
34
+ const [animate, setAnimate] = useState(false);
35
+
36
+ const segmentsRef = useRef<HTMLDivElement>(null);
37
+
38
+ const segmentsWithRefs = segments.map((segment) => ({
39
+ ...segment,
40
+ ref: createRef<HTMLLabelElement | HTMLButtonElement>(),
41
+ }));
42
+
43
+ const updateSegmentPosition = () => {
44
+ const selectedSegmentRef = segmentsWithRefs.find(
45
+ (segment) => segment.value === selectedValue,
46
+ )?.ref;
47
+
48
+ // We grab the active segments style object from the ref
49
+ // and set the css variables to the selected segments width and x position.
50
+ // This is so we can animate the highlight to the selected segment
51
+ if (selectedSegmentRef?.current && segmentsRef.current) {
52
+ const { style } = segmentsRef.current;
53
+ style.setProperty('--segment-highlight-width', `${selectedSegmentRef.current.offsetWidth}px`);
54
+ style.setProperty('--segment-highlight-x', `${selectedSegmentRef.current.offsetLeft}px`);
55
+ }
56
+ };
57
+
58
+ useEffect(() => {
59
+ updateSegmentPosition();
60
+ // eslint-disable-next-line react-hooks/exhaustive-deps
61
+ }, [segmentsWithRefs, selectedValue]);
62
+
63
+ useEffect(() => {
64
+ const handleWindowSizeChange = () => {
65
+ setAnimate(false);
66
+ updateSegmentPosition();
67
+ };
68
+
69
+ window.addEventListener('resize', handleWindowSizeChange);
70
+ return () => {
71
+ window.removeEventListener('resize', handleWindowSizeChange);
72
+ };
73
+
74
+ // eslint-disable-next-line react-hooks/exhaustive-deps
75
+ }, []);
76
+
77
+ useEffect(() => {
78
+ onChange(selectedValue);
79
+ }, [onChange, selectedValue]);
80
+
81
+ return (
82
+ <div
83
+ ref={segmentsRef}
84
+ className={classNames('segmented-control', {
85
+ 'segmented-control--input': type === 'input',
86
+ })}
87
+ >
88
+ <div
89
+ className={classNames('segmented-control__segments', {
90
+ 'segmented-control__segments--no-animate': !animate,
91
+ })}
92
+ >
93
+ {segmentsWithRefs.map((segment) =>
94
+ type === 'input' ? (
95
+ <label
96
+ ref={segment.ref as React.RefObject<HTMLLabelElement>}
97
+ key={segment.value}
98
+ className={classNames('segmented-control__segment', {
99
+ 'segmented-control__selected-segment': selectedValue === segment.value,
100
+ })}
101
+ htmlFor={segment.value}
102
+ >
103
+ <input
104
+ type="radio"
105
+ className="segmented-control__radio-input"
106
+ id={segment.value}
107
+ name={name}
108
+ value={segment.value}
109
+ checked={selectedValue === segment.value}
110
+ onChange={() => {
111
+ setAnimate(true);
112
+ setSelectedValue(segment.value);
113
+ }}
114
+ />
115
+ <div className="segmented-control__label-text">{segment.label}</div>
116
+ </label>
117
+ ) : (
118
+ <button
119
+ ref={segment.ref as React.RefObject<HTMLButtonElement>}
120
+ key={segment.value}
121
+ type="button"
122
+ aria-controls={controls}
123
+ className={classNames('segmented-control__segment', 'segmented-control__button', {
124
+ 'segmented-control__selected-segment': selectedValue === segment.value,
125
+ })}
126
+ onClick={() => {
127
+ setAnimate(true);
128
+ setSelectedValue(segment.value);
129
+ }}
130
+ >
131
+ {segment.label}
132
+ </button>
133
+ ),
134
+ )}
135
+ </div>
136
+ </div>
137
+ );
138
+ };
@@ -1,4 +1,5 @@
1
1
  @media (min-width: 768px) {
2
+ }@media (min-width: 768px) {
2
3
  }.np-select .np-dropdown-toggle {
3
4
  background-color: #ffffff;
4
5
  background-color: var(--color-background-screen);
@@ -7,6 +7,7 @@
7
7
  z-index: 1060;
8
8
  }
9
9
  [dir="rtl"] .snackbar {
10
+ right: 0;
10
11
  left: auto;
11
12
  left: initial;
12
13
  }
package/src/tile/Tile.css CHANGED
@@ -1,4 +1,5 @@
1
1
  @media (min-width: 768px) {
2
+ }@media (min-width: 768px) {
2
3
  }.np-tile {
3
4
  min-width: 120px;
4
5
  border-radius: 10px;
@@ -27,6 +28,10 @@
27
28
  }.np-tile__media {
28
29
  width: 100%;
29
30
  }.np-tile__description {
31
+ color: #5d7079;
32
+ color: var(--color-content-secondary);
33
+ margin-top: 12px;
34
+ margin-top: var(--size-12);
30
35
  display: inline-block;
31
36
  }.np-tile--small.np-decision__tile--fixed-width {
32
37
  width: 260px;