Skip to content

Commit 069e39d

Browse files
committed
refactor(cli): prefer recommended snippets
1 parent 870d30e commit 069e39d

File tree

6 files changed

+121
-29
lines changed

6 files changed

+121
-29
lines changed

.changeset/small-rockets-build.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
---
2+
"@chakra-ui/cli": minor
3+
---
4+
5+
Add support for `--all` flag to add all snippets. By default, we only install a
6+
minimal/recommended set of snippets.
7+
8+
> **Recommended snippets:** provider, avatar, button, checkbox, radio,
9+
> input-group, slider, popover, dialog, drawer, tooltip, field
10+
11+
If you want to add all snippets, you can use the `--all` flag.

.changeset/stupid-donkeys-learn.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,5 @@
66
rgb, hsb formats.
77

88
- Add new `ColorSwatch` component to preview a color.
9+
10+
- Fix issue where `mergeConfigs` mutates the underlying configs passed to it.

.changeset/three-jeans-breathe.md

Lines changed: 0 additions & 5 deletions
This file was deleted.

apps/www/content/docs/components/tooltip.mdx

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,13 @@ Use the `openDelay` and `closeDelay` prop to change the delay of the tooltip.
6060

6161
<ExampleTabs name="tooltip-with-delay" />
6262

63+
### Custom Background
64+
65+
Use the `--tooltip-bg` CSS variable to change the background color of the
66+
tooltip.
67+
68+
<ExampleTabs name="tooltip-with-custom-bg" />
69+
6370
### Controlled
6471

6572
Use the `open` and `onOpenChange` prop to control the visibility of the tooltip.
@@ -80,6 +87,38 @@ not be shown.
8087

8188
<ExampleTabs name="tooltip-with-disabled" />
8289

90+
### With Avatar
91+
92+
Here's an example of how to use the `Tooltip` component with an `Avatar`
93+
component.
94+
95+
<ExampleTabs name="tooltip-with-avatar" />
96+
97+
### With Checkbox
98+
99+
Here's an example of how to use the `Tooltip` component with a `Checkbox`
100+
component.
101+
102+
<ExampleTabs name="tooltip-with-checkbox" />
103+
104+
### With MenuItem
105+
106+
Here's an example of how to use the `Tooltip` with a `MenuItem` component.
107+
108+
<ExampleTabs name="tooltip-with-menu-item" />
109+
110+
### With Switch
111+
112+
Here's an example of how to wrap `Tooltip` around a `Switch` component.
113+
114+
<ExampleTabs name="tooltip-with-switch" />
115+
116+
### With Tabs
117+
118+
Here's an example of how to wrap `Tooltip` around a `Tabs` component.
119+
120+
<ExampleTabs name="tooltip-with-tab" />
121+
83122
## Props
84123

85124
### Root

packages/cli/src/commands/snippet.ts

Lines changed: 68 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -11,26 +11,16 @@ import { fetchComposition, fetchCompositions } from "../utils/fetch"
1111
import { getFileDependencies } from "../utils/get-file-dependencies"
1212
import { ensureDir } from "../utils/io"
1313
import { installCommand } from "../utils/run-command"
14-
import { type CompositionFile, addCommandFlagsSchema } from "../utils/schema"
14+
import {
15+
type CompositionFile,
16+
type Compositions,
17+
addCommandFlagsSchema,
18+
} from "../utils/schema"
1519
import { uniq } from "../utils/shared"
1620
import { tasks } from "../utils/tasks"
1721

1822
const debug = createDebug("chakra:snippet")
1923

20-
async function transformToJsx(item: CompositionFile) {
21-
const content = await convertTsxToJsx(item.file.content)
22-
item.file.content = content
23-
item.file.name = item.file.name.replace(".tsx", ".jsx")
24-
}
25-
26-
function printFileSync(item: CompositionFile) {
27-
const boxText = boxen(item.file.content, {
28-
headerText: `${item.file.name}\n`,
29-
borderStyle: "none",
30-
})
31-
p.log.info(boxText)
32-
}
33-
3424
export const SnippetCommand = new Command("snippet")
3525
.description("Add snippets to your project for better DX")
3626
.addCommand(
@@ -39,10 +29,11 @@ export const SnippetCommand = new Command("snippet")
3929
.argument("[snippets...]", "snippets to add")
4030
.option("-d, --dry-run", "Dry run")
4131
.option("--outdir <dir>", "Output directory to write the snippets")
32+
.option("--all", "Add all snippets")
4233
.option("-f, --force", "Overwrite existing files")
43-
.action(async (components: string[], flags: unknown) => {
34+
.action(async (selectedComponents: string[], flags: unknown) => {
4435
const parsedFlags = addCommandFlagsSchema.parse(flags)
45-
const { dryRun, force } = parsedFlags
36+
const { dryRun, force, all } = parsedFlags
4637

4738
const ctx = await getProjectContext({ cwd: process.cwd() })
4839
debug("context", ctx)
@@ -54,16 +45,16 @@ export const SnippetCommand = new Command("snippet")
5445

5546
const items = await fetchCompositions()
5647

57-
const all = components.length === 0
58-
59-
if (all) {
60-
p.log.info("No components selected, Adding all...")
61-
components = items.map((item) => item.id)
62-
}
48+
const inferredComponents = getComponents({
49+
components: selectedComponents,
50+
all,
51+
items,
52+
})
6353

54+
const components = inferredComponents.items
6455
debug("components", components)
6556

66-
p.log.info(`Adding ${components.length} snippet(s)...`)
57+
p.log.info(inferredComponents.message)
6758

6859
const deps = uniq(
6960
components.flatMap((id) => getFileDependencies(items, id)),
@@ -191,3 +182,56 @@ export const SnippetCommand = new Command("snippet")
191182
p.outro("🎉 Done!")
192183
}),
193184
)
185+
186+
async function transformToJsx(item: CompositionFile) {
187+
const content = await convertTsxToJsx(item.file.content)
188+
item.file.content = content
189+
item.file.name = item.file.name.replace(".tsx", ".jsx")
190+
}
191+
192+
function printFileSync(item: CompositionFile) {
193+
const boxText = boxen(item.file.content, {
194+
headerText: `${item.file.name}\n`,
195+
borderStyle: "none",
196+
})
197+
p.log.info(boxText)
198+
}
199+
200+
const RECOMMENDED_SNIPPETS = [
201+
"provider",
202+
"avatar",
203+
"button",
204+
"checkbox",
205+
"radio",
206+
"input-group",
207+
"slider",
208+
"popover",
209+
"dialog",
210+
"drawer",
211+
"tooltip",
212+
"field",
213+
]
214+
215+
function getComponents(opts: {
216+
components: string[]
217+
all: boolean | undefined
218+
items: Compositions
219+
}) {
220+
const { components, all, items } = opts
221+
if (components.length === 0 && !all)
222+
return {
223+
message: "No component(s) selected, adding recommended snippets...",
224+
items: RECOMMENDED_SNIPPETS,
225+
}
226+
227+
if (all)
228+
return {
229+
message: "Adding all snippets...",
230+
items: items.map((item) => item.id),
231+
}
232+
233+
return {
234+
message: `Adding ${components.length} snippet(s)...`,
235+
items: components,
236+
}
237+
}

packages/cli/src/utils/schema.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ export const addCommandFlagsSchema = z.object({
3838
force: z.boolean().optional(),
3939
dryRun: z.boolean().optional(),
4040
outdir: z.string().optional(),
41+
all: z.boolean().optional(),
4142
})
4243

4344
export interface AddCommandFlags

0 commit comments

Comments
 (0)