starlight-package-managers 0.4.0 → 0.6.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.
@@ -38,131 +38,12 @@ function getTabItemProps(pkgManager: PackageManager) {
38
38
  singlePkgManager ? (
39
39
  <Code code={getCommand(singlePkgManager, type, pkg, options)} lang="sh" {title} frame={ecFrame} />
40
40
  ) : (
41
- <starlight-package-managers>
42
- <Tabs>
43
- {getSupportedPkgManagers(type, pkgManagers).map((pkgManager) => (
44
- <TabItem {...getTabItemProps(pkgManager)}>
45
- <Code code={getCommand(pkgManager, type, pkg, options)} lang="sh" {title} frame={ecFrame} />
46
- </TabItem>
47
- ))}
48
- </Tabs>
49
- </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>
50
48
  )
51
49
  }
52
-
53
- <style>
54
- starlight-package-managers {
55
- display: block;
56
- }
57
- </style>
58
-
59
- <script>
60
- customElements.define(
61
- 'starlight-package-managers',
62
- class StarlightPackageManagers extends HTMLElement {
63
- #observer: MutationObserver
64
- #observerConfig: MutationObserverInit = { attributes: true, attributeFilter: ['aria-selected'] }
65
-
66
- #tabs: NodeListOf<HTMLAnchorElement>
67
-
68
- constructor() {
69
- super()
70
-
71
- this.#observer = new MutationObserver(this.#handleTabChange)
72
- this.#tabs = this.querySelectorAll<HTMLAnchorElement>('starlight-tabs [role="tablist"] [role="tab"]')
73
-
74
- this.observeTabs()
75
- }
76
-
77
- observeTabs = () => {
78
- for (const tab of this.#tabs) {
79
- this.#observer.observe(tab, this.#observerConfig)
80
- }
81
- }
82
-
83
- disconnectTabs = () => {
84
- this.#observer.disconnect()
85
- }
86
-
87
- #handleTabChange = (mutations: MutationRecord[]) => {
88
- const pkgTabs = [...document.querySelectorAll<this>('starlight-package-managers')]
89
-
90
- for (const pkgTab of pkgTabs) {
91
- pkgTab.disconnectTabs()
92
- }
93
-
94
- let newFocusedTab: HTMLAnchorElement | null = null
95
-
96
- for (const mutation of mutations) {
97
- if (mutation.attributeName !== 'aria-selected' || !(mutation.target instanceof HTMLAnchorElement)) {
98
- continue
99
- }
100
-
101
- if (mutation.target.getAttribute('aria-selected') !== 'true') {
102
- continue
103
- }
104
-
105
- newFocusedTab = mutation.target
106
-
107
- const newPkgManager = newFocusedTab.textContent?.trim() ?? ''
108
- const otherTabs = pkgTabs.filter((tabs) => tabs !== this)
109
-
110
- for (const otherTab of otherTabs) {
111
- const starlightTabs = otherTab.querySelector('starlight-tabs')
112
-
113
- if (!this.#isStarlightTabs(starlightTabs)) {
114
- continue
115
- }
116
-
117
- // We cannot use the `switchTab()` method provided by Starlight as it focuses the new tab which will cause
118
- // the page to scroll for every set of tabs.
119
- this.#switchTabs(starlightTabs, newPkgManager)
120
- }
121
- }
122
-
123
- requestAnimationFrame(() => {
124
- for (const pkgTab of pkgTabs) {
125
- pkgTab.observeTabs()
126
- }
127
-
128
- newFocusedTab?.focus()
129
- })
130
- }
131
-
132
- // https://github.com/withastro/starlight/blob/290af4f0b5f65619fa4c65609940c3a911a7e698/packages/starlight/user-components/Tabs.astro#L116-L135
133
- #switchTabs(starlightTabs: StarlightTabs, newPkgManager: string) {
134
- const tabs = starlightTabs.tabs
135
- const newTabIndex = tabs.findIndex((tab) => tab.textContent?.trim() === newPkgManager)
136
- const newTab = tabs[newTabIndex]
137
- const newPanel = starlightTabs.panels[newTabIndex]
138
-
139
- if (!newTab || !newPanel) {
140
- return
141
- }
142
-
143
- for (const tab of starlightTabs.tabs) {
144
- tab.removeAttribute('aria-selected')
145
- tab.setAttribute('tabindex', '-1')
146
- }
147
-
148
- for (const panel of starlightTabs.panels) {
149
- panel.hidden = true
150
- }
151
-
152
- newPanel.hidden = false
153
- // Restore active tab to the default tab order.
154
- newTab.removeAttribute('tabindex')
155
- newTab.setAttribute('aria-selected', 'true')
156
- }
157
-
158
- #isStarlightTabs(element: Element | null): element is StarlightTabs {
159
- return element instanceof Element && 'tabs' in element && 'panels' in element
160
- }
161
- },
162
- )
163
-
164
- type StarlightTabs = Element & {
165
- panels: HTMLElement[]
166
- tabs: HTMLAnchorElement[]
167
- }
168
- </script>
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "starlight-package-managers",
3
- "version": "0.4.0",
3
+ "version": "0.6.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,13 +10,13 @@
10
10
  "./package.json": "./package.json"
11
11
  },
12
12
  "devDependencies": {
13
- "@astrojs/starlight": "0.21.0",
13
+ "@astrojs/starlight": "0.22.0",
14
14
  "@playwright/test": "1.36.2",
15
15
  "astro": "4.3.7",
16
16
  "vitest": "0.33.0"
17
17
  },
18
18
  "peerDependencies": {
19
- "@astrojs/starlight": ">=0.21.0",
19
+ "@astrojs/starlight": ">=0.22.0",
20
20
  "astro": ">=4.2.7"
21
21
  },
22
22
  "engines": {
package/pkg.ts CHANGED
@@ -9,6 +9,7 @@ const commands: Commands = {
9
9
  devOption: '-D',
10
10
  exec: 'npx',
11
11
  run: 'npm run',
12
+ remove: 'npm uninstall',
12
13
  },
13
14
  yarn: {
14
15
  add: 'yarn add',
@@ -16,6 +17,7 @@ const commands: Commands = {
16
17
  devOption: '-D',
17
18
  exec: 'yarn',
18
19
  run: 'yarn run',
20
+ remove: 'yarn remove',
19
21
  },
20
22
  pnpm: {
21
23
  add: 'pnpm add',
@@ -23,18 +25,21 @@ const commands: Commands = {
23
25
  devOption: '-D',
24
26
  exec: 'pnpm',
25
27
  run: 'pnpm run',
28
+ remove: 'pnpm remove',
26
29
  },
27
30
  bun: {
28
31
  add: 'bun add',
29
32
  devOption: '-d',
30
33
  exec: 'bunx',
31
34
  run: 'bun run',
35
+ remove: 'bun remove',
32
36
  },
33
37
  ni: {
34
38
  add: 'ni',
35
39
  devOption: '-D',
36
40
  exec: 'nlx',
37
41
  run: 'nr',
42
+ remove: 'nun',
38
43
  },
39
44
  }
40
45
 
@@ -93,7 +98,7 @@ export function getCommand(
93
98
  return command
94
99
  }
95
100
 
96
- export type CommandType = 'add' | 'create' | 'exec' | 'run'
101
+ export type CommandType = 'add' | 'create' | 'exec' | 'run' | 'remove'
97
102
 
98
103
  export interface CommandOptions {
99
104
  args?: string