starlight-package-managers 0.3.0 → 0.5.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.
@@ -1,152 +1,49 @@
1
1
  ---
2
2
  import { Code, Tabs, TabItem } from '@astrojs/starlight/components'
3
+ import type { ComponentProps } from 'astro/types'
3
4
 
4
- import { type CommandType, getCommand, type CommandOptions, getSupportedPkgManagers, type PackageManager } from './pkg'
5
+ import {
6
+ type CommandOptions,
7
+ type CommandType,
8
+ getCommand,
9
+ getSupportedPkgManagers,
10
+ type PackageManager,
11
+ getIcon,
12
+ } from './pkg'
5
13
 
6
14
  export type PackageManagersProps = Props
7
15
 
8
16
  interface Props extends CommandOptions {
9
17
  frame?: 'none' | 'terminal'
18
+ icons?: boolean
10
19
  pkg?: string | undefined
11
20
  pkgManagers?: PackageManager[]
12
21
  title?: string | undefined
13
22
  type?: CommandType
14
23
  }
15
24
 
16
- const { frame = 'terminal', pkg, pkgManagers, title = undefined, type = 'add', ...options } = Astro.props
25
+ const { frame = 'terminal', icons = true, pkg, pkgManagers, title = undefined, type = 'add', ...options } = Astro.props
17
26
  const singlePkgManager = pkgManagers?.length === 1 ? pkgManagers[0] : undefined
18
27
  const ecFrame = frame === 'terminal' ? 'terminal' : 'code'
28
+
29
+ function getTabItemProps(pkgManager: PackageManager) {
30
+ const props: ComponentProps<typeof TabItem> = { label: pkgManager }
31
+ const icon = getIcon(pkgManager) as ComponentProps<typeof TabItem>['icon']
32
+ if (icons && icon) props.icon = icon
33
+ return props
34
+ }
19
35
  ---
20
36
 
21
37
  {
22
38
  singlePkgManager ? (
23
39
  <Code code={getCommand(singlePkgManager, type, pkg, options)} lang="sh" {title} frame={ecFrame} />
24
40
  ) : (
25
- <starlight-package-managers>
26
- <Tabs>
27
- {getSupportedPkgManagers(type, pkgManagers).map((pkgManager) => (
28
- <TabItem label={pkgManager}>
29
- <Code code={getCommand(pkgManager, type, pkg, options)} lang="sh" {title} frame={ecFrame} />
30
- </TabItem>
31
- ))}
32
- </Tabs>
33
- </starlight-package-managers>
41
+ <Tabs syncKey="starlight-package-managers-pkg">
42
+ {getSupportedPkgManagers(type, pkgManagers).map((pkgManager) => (
43
+ <TabItem {...getTabItemProps(pkgManager)}>
44
+ <Code code={getCommand(pkgManager, type, pkg, options)} lang="sh" {title} frame={ecFrame} />
45
+ </TabItem>
46
+ ))}
47
+ </Tabs>
34
48
  )
35
49
  }
36
-
37
- <style>
38
- starlight-package-managers {
39
- display: block;
40
- }
41
- </style>
42
-
43
- <script>
44
- customElements.define(
45
- 'starlight-package-managers',
46
- class StarlightPackageManagers extends HTMLElement {
47
- #observer: MutationObserver
48
- #observerConfig: MutationObserverInit = { attributes: true, attributeFilter: ['aria-selected'] }
49
-
50
- #tabs: NodeListOf<HTMLAnchorElement>
51
-
52
- constructor() {
53
- super()
54
-
55
- this.#observer = new MutationObserver(this.#handleTabChange)
56
- this.#tabs = this.querySelectorAll<HTMLAnchorElement>('starlight-tabs [role="tablist"] [role="tab"]')
57
-
58
- this.observeTabs()
59
- }
60
-
61
- observeTabs = () => {
62
- for (const tab of this.#tabs) {
63
- this.#observer.observe(tab, this.#observerConfig)
64
- }
65
- }
66
-
67
- disconnectTabs = () => {
68
- this.#observer.disconnect()
69
- }
70
-
71
- #handleTabChange = (mutations: MutationRecord[]) => {
72
- const pkgTabs = [...document.querySelectorAll<this>('starlight-package-managers')]
73
-
74
- for (const pkgTab of pkgTabs) {
75
- pkgTab.disconnectTabs()
76
- }
77
-
78
- let newFocusedTab: HTMLAnchorElement | null = null
79
-
80
- for (const mutation of mutations) {
81
- if (mutation.attributeName !== 'aria-selected' || !(mutation.target instanceof HTMLAnchorElement)) {
82
- continue
83
- }
84
-
85
- if (mutation.target.getAttribute('aria-selected') !== 'true') {
86
- continue
87
- }
88
-
89
- newFocusedTab = mutation.target
90
-
91
- const newPkgManager = newFocusedTab.textContent?.trim() ?? ''
92
- const otherTabs = pkgTabs.filter((tabs) => tabs !== this)
93
-
94
- for (const otherTab of otherTabs) {
95
- const starlightTabs = otherTab.querySelector('starlight-tabs')
96
-
97
- if (!this.#isStarlightTabs(starlightTabs)) {
98
- continue
99
- }
100
-
101
- // We cannot use the `switchTab()` method provided by Starlight as it focuses the new tab which will cause
102
- // the page to scroll for every set of tabs.
103
- this.#switchTabs(starlightTabs, newPkgManager)
104
- }
105
- }
106
-
107
- requestAnimationFrame(() => {
108
- for (const pkgTab of pkgTabs) {
109
- pkgTab.observeTabs()
110
- }
111
-
112
- newFocusedTab?.focus()
113
- })
114
- }
115
-
116
- // https://github.com/withastro/starlight/blob/290af4f0b5f65619fa4c65609940c3a911a7e698/packages/starlight/user-components/Tabs.astro#L116-L135
117
- #switchTabs(starlightTabs: StarlightTabs, newPkgManager: string) {
118
- const tabs = starlightTabs.tabs
119
- const newTabIndex = tabs.findIndex((tab) => tab.textContent?.trim() === newPkgManager)
120
- const newTab = tabs[newTabIndex]
121
- const newPanel = starlightTabs.panels[newTabIndex]
122
-
123
- if (!newTab || !newPanel) {
124
- return
125
- }
126
-
127
- for (const tab of starlightTabs.tabs) {
128
- tab.removeAttribute('aria-selected')
129
- tab.setAttribute('tabindex', '-1')
130
- }
131
-
132
- for (const panel of starlightTabs.panels) {
133
- panel.hidden = true
134
- }
135
-
136
- newPanel.hidden = false
137
- // Restore active tab to the default tab order.
138
- newTab.removeAttribute('tabindex')
139
- newTab.setAttribute('aria-selected', 'true')
140
- }
141
-
142
- #isStarlightTabs(element: Element | null): element is StarlightTabs {
143
- return element instanceof Element && 'tabs' in element && 'panels' in element
144
- }
145
- },
146
- )
147
-
148
- type StarlightTabs = Element & {
149
- panels: HTMLElement[]
150
- tabs: HTMLAnchorElement[]
151
- }
152
- </script>
package/README.md CHANGED
@@ -2,8 +2,8 @@
2
2
  <h1>starlight-package-managers 🗃</h1>
3
3
  <p>Quickly display npm related commands for multiple package managers in your Starlight documentation site.</p>
4
4
  <p>
5
- <a href="https://github.com/HiDeoo/vercel-env-push/assets/494699/12a27480-f3ff-470f-a108-e1bf73cd9146" title="Demo of starlight-package-managers">
6
- <img alt="Demo of starlight-package-managers" src="https://github.com/HiDeoo/vercel-env-push/assets/494699/12a27480-f3ff-470f-a108-e1bf73cd9146" width="520" />
5
+ <a href="https://github.com/HiDeoo/starlight-package-managers/assets/494699/5666eb82-4e80-4ce9-ac07-3f711e51062c" title="Demo of starlight-package-managers">
6
+ <img alt="Demo of starlight-package-managers" src="https://github.com/HiDeoo/starlight-package-managers/assets/494699/5666eb82-4e80-4ce9-ac07-3f711e51062c" width="520" />
7
7
  </a>
8
8
  </p>
9
9
  </div>
@@ -20,7 +20,7 @@
20
20
 
21
21
  ## Getting Started
22
22
 
23
- Want to get started immediately? Check out the [getting started guide](https://starlight-package-managers.vercel.app/guides/getting-started/) or a [live demo](https://starlight-package-managers.vercel.app/demo/).
23
+ Want to get started immediately? Check out the [getting started guide](https://starlight-package-managers.vercel.app/getting-started/) or a [live demo](https://starlight-package-managers.vercel.app/demo/).
24
24
 
25
25
  ## Description
26
26
 
@@ -61,9 +61,9 @@ By this one:
61
61
  ## Features
62
62
 
63
63
  - Support for various package managers: [npm](https://www.npmjs.com), [yarn](https://yarnpkg.com), [pnpm](https://pnpm.io), [bun](https://bun.sh) & [ni](https://github.com/antfu/ni).
64
- - Support for various types of command: [`add`](https://starlight-package-managers.vercel.app/guides/usage/#add), [`create`](https://starlight-package-managers.vercel.app/guides/usage/#create), [`exec`](https://starlight-package-managers.vercel.app/guides/usage/#exec) & [`run`](https://starlight-package-managers.vercel.app/guides/usage/#run).
64
+ - Support for various types of command: [`add`](https://starlight-package-managers.vercel.app/usage/#add), [`create`](https://starlight-package-managers.vercel.app/usage/#create), [`exec`](https://starlight-package-managers.vercel.app/usage/#exec) & [`run`](https://starlight-package-managers.vercel.app/usage/#run).
65
65
  - Synced tabs between each instance on the same page.
66
- - Customizable output with [extra arguments](https://starlight-package-managers.vercel.app/guides/usage/#extra-arguments), [comments](https://starlight-package-managers.vercel.app/guides/usage/#comment) & [prefixes](https://starlight-package-managers.vercel.app/guides/usage/#prefix).
66
+ - Customizable output with [extra arguments](https://starlight-package-managers.vercel.app/usage/#extra-arguments), [comments](https://starlight-package-managers.vercel.app/usage/#comment) & [prefixes](https://starlight-package-managers.vercel.app/usage/#prefix).
67
67
 
68
68
  ## Documentation
69
69
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "starlight-package-managers",
3
- "version": "0.3.0",
3
+ "version": "0.5.0",
4
4
  "license": "MIT",
5
5
  "description": "Quickly display npm related commands for multiple package managers in your Starlight documentation site.",
6
6
  "author": "HiDeoo <github@hideoo.dev> (https://hideoo.dev)",
@@ -10,14 +10,14 @@
10
10
  "./package.json": "./package.json"
11
11
  },
12
12
  "devDependencies": {
13
- "@astrojs/starlight": "0.17.0",
13
+ "@astrojs/starlight": "0.22.0",
14
14
  "@playwright/test": "1.36.2",
15
- "astro": "4.2.6",
15
+ "astro": "4.3.7",
16
16
  "vitest": "0.33.0"
17
17
  },
18
18
  "peerDependencies": {
19
- "@astrojs/starlight": ">=0.17.0",
20
- "astro": ">=4.0.0"
19
+ "@astrojs/starlight": ">=0.22.0",
20
+ "astro": ">=4.2.7"
21
21
  },
22
22
  "engines": {
23
23
  "node": ">=18.14.1"
package/pkg.ts CHANGED
@@ -38,10 +38,22 @@ const commands: Commands = {
38
38
  },
39
39
  }
40
40
 
41
+ const icons: Record<PackageManager, string | undefined> = {
42
+ npm: 'seti:npm',
43
+ yarn: 'seti:yarn',
44
+ pnpm: 'pnpm',
45
+ bun: 'bun',
46
+ ni: undefined,
47
+ }
48
+
41
49
  export function getSupportedPkgManagers(type: CommandType, userPkgManagers: PackageManager[] | undefined) {
42
50
  return (userPkgManagers ?? defaultPkgManagers).filter((pkgManager) => commands[pkgManager][type] !== undefined)
43
51
  }
44
52
 
53
+ export function getIcon(pkgManager: PackageManager) {
54
+ return icons[pkgManager]
55
+ }
56
+
45
57
  export function getCommand(
46
58
  pkgManager: PackageManager,
47
59
  type: CommandType,