Skip to content

Commit 61bd727

Browse files
committed
feat(CopySnippet): improve component
1 parent 3b238e8 commit 61bd727

File tree

4 files changed

+118
-65
lines changed

4 files changed

+118
-65
lines changed

.changeset/new-bees-smoke.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@cube-dev/ui-kit': minor
3+
---
4+
5+
Simplify API of CopySnippet and improve its design.

src/components/content/CopySnippet/CopySnippet.stories.tsx

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,60 @@ JavascriptSyntax.args = {
7272
sql: \`SELECT * FROM public.line_items\`,
7373
7474
75+
joins: {
76+
Products: {
77+
sql: \`\${CUBE}.product_id = \${Products}.id\`,
78+
relationship: \`belongsTo\`
79+
},
80+
81+
Orders: {
82+
sql: \`\${CUBE}.order_id = \${Orders}.id\`,
83+
relationship: \`belongsTo\`
84+
}
85+
},
86+
87+
measures: {
88+
count: {
89+
type: \`count\`,
90+
drillMembers: [id, createdAt]
91+
},
92+
93+
price: {
94+
sql: \`price\`,
95+
type: \`sum\`
96+
},
97+
98+
quantity: {
99+
sql: \`quantity\`,
100+
type: \`sum\`
101+
}
102+
},
103+
104+
dimensions: {
105+
id: {
106+
sql: \`id\`,
107+
type: \`number\`,
108+
primaryKey: true
109+
},
110+
111+
createdAt: {
112+
sql: \`created_at\`,
113+
type: \`time\`
114+
}
115+
}
116+
});`,
117+
};
118+
119+
export const Complex = Template.bind({});
120+
Complex.args = {
121+
language: 'javascript',
122+
height: 'max 30x',
123+
hideText: 'SELECT * FROM public.line_items',
124+
actions: <CopySnippet.Button icon={<SettingOutlined />} />,
125+
code: `cube('LineItems', {
126+
sql: \`SELECT * FROM public.line_items\`,
127+
128+
75129
joins: {
76130
Products: {
77131
sql: \`\${CUBE}.product_id = \${Products}.id\`,

src/components/content/CopySnippet/CopySnippet.tsx

Lines changed: 57 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
import { ReactNode, useState } from 'react';
1+
import { ReactNode, useMemo, useState } from 'react';
22
import copy from 'clipboard-copy';
33

44
import { Action, Button } from '../../actions';
55
import { Card, CubeCardProps } from '../Card/Card';
6-
import { Styles, tasty } from '../../../tasty';
6+
import { tasty } from '../../../tasty';
77
import {
88
CubePrismCodeProps,
99
PrismCode,
@@ -24,16 +24,21 @@ const ActionElement = tasty(Action, {
2424
const StyledBlock = tasty({
2525
styles: {
2626
position: 'relative',
27-
overflow: {
28-
'': 'hidden',
29-
scroll: 'auto hidden',
30-
},
27+
overflow: 'auto',
3128
maxWidth: '100%',
3229
whiteSpace: {
3330
'': 'initial',
3431
nowrap: 'nowrap',
3532
},
3633
styledScrollbar: true,
34+
padding: {
35+
'': '1x 1.5x 0 1x',
36+
multiline: '1x 1.5x',
37+
},
38+
fade: {
39+
'': 'right 2x',
40+
multiline: false,
41+
},
3742

3843
'& code': {
3944
font: {
@@ -47,42 +52,45 @@ const StyledBlock = tasty({
4752
const ButtonContainer = tasty({
4853
styles: {
4954
position: 'relative',
50-
display: 'grid',
51-
gridAutoFlow: 'column',
52-
margin: '(-1x - 1bw) -1.5x (-1x - 1bw) -1x',
55+
display: 'flex',
56+
flow: {
57+
'': 'row-reverse',
58+
multiline: 'column',
59+
},
60+
placeContent: 'start',
5361

54-
'&::after': {
55-
display: {
56-
'': 'none',
57-
overlay: 'block',
58-
},
59-
content: '""',
60-
width: '2x',
61-
position: 'absolute',
62-
left: '-1x',
63-
top: 0,
64-
bottom: 0,
65-
backgroundImage:
66-
'linear-gradient(to right,rgba(var(--context-fill-color-rgb),0),rgba(var(--context-fill-color-rgb),1))',
62+
'@button-size': {
63+
'': '5x',
64+
multiline: '4x',
6765
},
66+
67+
'@button-radius': {
68+
'': '0',
69+
multiline: '1r',
70+
},
71+
72+
// border: 'left',
6873
},
6974
});
7075

7176
const CopySnippetElement = tasty(Card, {
7277
qa: 'CopySnippet',
7378
styles: {
74-
display: 'block',
79+
display: 'grid',
80+
gridRows: 'minmax(0, 1fr)',
7581
fill: '#grey-light',
76-
radius: '@large-radius',
7782
border: 0,
7883
padding: 0,
84+
preset: 'default',
85+
radius: '1r',
7986

8087
Grid: {
8188
display: 'grid',
8289
flow: 'row',
8390
gridColumns: 'minmax(0, 1fr) auto',
91+
gridRows: 'minmax(0, 1fr)',
8492
width: 'min 20x',
85-
radius: '@large-radius',
93+
radius: '1r',
8694
position: 'relative',
8795
overflow: 'hidden',
8896
},
@@ -92,40 +100,34 @@ const CopySnippetElement = tasty(Card, {
92100
const ActionButton = tasty(Button, {
93101
type: 'clear',
94102
styles: {
95-
padding: '1x 1.5x',
96-
radius: 0,
97-
placeSelf: {
98-
'': 'none',
99-
'!multiline & !with-scroll': 'stretch',
100-
},
103+
width: '4x',
104+
padding: 0,
105+
placeSelf: 'stretch',
101106
border: '#clear',
102107
outline: {
103108
'': '#purple-03.0',
104109
'focused & focus-visible': '#purple-03 inset',
105110
},
111+
height: '@button-size',
112+
radius: {
113+
'': '0',
114+
':last-child': '0 0 0 @button-radius',
115+
':first-child': '0 @button-radius 0 0',
116+
':last-child & :first-child': '0 @button-radius 0 @button-radius',
117+
},
106118
},
107119
});
108120

109121
const CopyButton = tasty(ActionButton, {
110122
icon: <CopyIcon />,
111123
'aria-label': 'Copy to clipboard',
112-
styles: {
113-
radius: {
114-
'': '0 1r 1r 0',
115-
'multiline | with-scroll': '0 1r 0 0',
116-
},
117-
},
118124
});
119125

120126
const ShowButton = tasty(ActionButton, {
121127
'aria-label': 'Show hidden parts',
122-
styles: {
123-
radius: 0,
124-
},
125128
});
126129

127130
export interface CubeCopySnippetProps extends CubeCardProps {
128-
padding?: Styles['padding'];
129131
/** The code snippet */
130132
code: string;
131133
/** The title of the snippet */
@@ -138,10 +140,6 @@ export interface CubeCopySnippetProps extends CubeCardProps {
138140
language?: CubePrismCodeProps['language'];
139141
/** Whether the snippet uses a serif font */
140142
serif?: boolean;
141-
/** Whether the snippet uses overlay on the edge */
142-
showOverlay?: boolean;
143-
/** Whether the snippet is scrollable */
144-
showScroll?: boolean;
145143
/** Whether to show the tooltip with the full content */
146144
showTooltip?: boolean;
147145
hideText?: string[] | string | boolean;
@@ -162,13 +160,9 @@ function CopySnippet(allProps: CubeCopySnippetProps) {
162160
nowrap,
163161
prefix = '',
164162
language,
165-
showScroll = true,
166163
serif,
167164
actions,
168-
padding = '1.125x 1.5x',
169-
showOverlay = true,
170165
showTooltip = false,
171-
styles,
172166
hideText,
173167
...props
174168
} = allProps;
@@ -210,36 +204,34 @@ function CopySnippet(allProps: CubeCopySnippetProps) {
210204
}
211205
}
212206

207+
const mods = useMemo(() => {
208+
return {
209+
nowrap,
210+
multiline,
211+
serif,
212+
hidden: !!hideText,
213+
};
214+
}, [nowrap, multiline, hideText, serif]);
215+
213216
const Snippet = (
214-
<CopySnippetElement styles={{ preset: 'default', ...styles }} {...props}>
217+
<CopySnippetElement mods={mods} {...props}>
215218
<div data-element="Grid">
216-
<StyledBlock
217-
mods={{ nowrap, multiline, scroll: showScroll, serif }}
218-
styles={{ padding }}
219-
>
219+
<StyledBlock mods={mods}>
220220
<PrismCode
221221
style={{ margin: 0, overflow: 'visible' }}
222222
code={formattedCode}
223223
language={language || 'javascript'}
224224
/>
225225
</StyledBlock>
226-
<ButtonContainer
227-
styles={{ padding } as Styles}
228-
mods={{ overlay: showOverlay }}
229-
>
230-
{actions}
226+
<ButtonContainer mods={mods}>
227+
<CopyButton aria-label={`Copy ${title}`} onPress={onCopy} />
231228
{hideText && (
232229
<ShowButton
233230
icon={showHidden ? <EyeInvisibleIcon /> : <EyeIcon />}
234-
mods={{ multiline, withScroll: showScroll }}
235231
onPress={() => setShowHidden(!showHidden)}
236232
/>
237233
)}
238-
<CopyButton
239-
aria-label={`Copy ${title}`}
240-
mods={{ multiline, withScroll: showScroll, hidden: !!hideText }}
241-
onPress={onCopy}
242-
/>
234+
{actions}
243235
</ButtonContainer>
244236
</div>
245237
</CopySnippetElement>

src/components/content/PrismCode/PrismCode.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ const PreElement = tasty({
1717
styles: {
1818
margin: 0,
1919
padding: 0,
20+
overflow: 'auto',
21+
styledScrollbar: true,
2022

2123
Code: {
2224
display: 'block',

0 commit comments

Comments
 (0)