Skip to content

Commit e613b54

Browse files
kairi003aklinker1
andauthored
feat: add default_state option to popup actions (#2010)
Co-authored-by: Aaron <aaronklinker1@gmail.com>
1 parent d9c1401 commit e613b54

3 files changed

Lines changed: 78 additions & 0 deletions

File tree

packages/wxt/src/core/utils/__tests__/manifest.test.ts

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import {
1818
ContentScriptEntrypoint,
1919
Entrypoint,
2020
OutputAsset,
21+
TargetManifestVersion,
2122
} from '../../../types';
2223
import { wxt } from '../../wxt';
2324
import { mock } from 'vitest-mock-extended';
@@ -198,6 +199,74 @@ describe('Manifest Utils', () => {
198199
expect((actual.action as any).theme_icons).toEqual(themeIcons);
199200
});
200201

202+
describe('default_state', () => {
203+
it.each<{
204+
browser: string;
205+
manifestVersion: TargetManifestVersion;
206+
type: 'browser_action' | 'action' | 'page_action';
207+
shouldExist: boolean;
208+
}>([
209+
{
210+
browser: 'chrome',
211+
manifestVersion: 2,
212+
type: 'browser_action',
213+
shouldExist: false,
214+
},
215+
{
216+
browser: 'chrome',
217+
manifestVersion: 3,
218+
type: 'action',
219+
shouldExist: true,
220+
},
221+
{
222+
browser: 'firefox',
223+
manifestVersion: 2,
224+
type: 'browser_action',
225+
shouldExist: false,
226+
},
227+
{
228+
browser: 'firefox',
229+
manifestVersion: 3,
230+
type: 'action',
231+
shouldExist: true,
232+
},
233+
])(
234+
'should configure default_state: $defaultState based on the $browser and mv$manifestVersion',
235+
async ({ browser, manifestVersion, type, shouldExist }) => {
236+
const popup = fakePopupEntrypoint({
237+
options: {
238+
// @ts-expect-error: Force this to be undefined when null
239+
actionType: manifestVersion === 3 ? null : type,
240+
defaultState: 'enabled',
241+
},
242+
outputDir: outDir,
243+
skipped: false,
244+
});
245+
const buildOutput = fakeBuildOutput();
246+
setFakeWxt({
247+
config: {
248+
browser,
249+
manifestVersion,
250+
outDir,
251+
},
252+
});
253+
254+
const { manifest: actual } = await generateManifest(
255+
[popup],
256+
buildOutput,
257+
);
258+
259+
if (shouldExist) {
260+
expect(actual[type]).toMatchObject({
261+
default_state: 'enabled',
262+
});
263+
} else {
264+
expect(actual[type].default_state).toBeUndefined();
265+
}
266+
},
267+
);
268+
});
269+
201270
it('should include default_area for Firefox in mv2', async () => {
202271
const popup = fakePopupEntrypoint({
203272
options: {

packages/wxt/src/core/utils/manifest.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,9 @@ function addEntrypoints(
294294
options.default_icon = popup.options.defaultIcon;
295295
if (popup.options.defaultTitle)
296296
options.default_title = popup.options.defaultTitle;
297+
if (popup.options.defaultState && wxt.config.manifestVersion === 3)
298+
// @ts-expect-error: Not typed by @wxt-dev/browser, but supported by Chrome
299+
options.default_state = popup.options.defaultState;
297300
if (popup.options.browserStyle)
298301
// @ts-expect-error: Not typed by @wxt-dev/browser, but supported by Firefox
299302
options.browser_style = popup.options.browserStyle;

packages/wxt/src/types.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -758,6 +758,12 @@ export interface PopupEntrypointOptions extends BaseEntrypointOptions {
758758
mv2Key?: PerBrowserOption<'browser_action' | 'page_action'>;
759759
defaultIcon?: Record<string, string>;
760760
defaultTitle?: PerBrowserOption<string>;
761+
/**
762+
* Chrome only. Controls the initial enabled/disabled state of the action.
763+
*
764+
* @see https://developer.chrome.com/docs/extensions/reference/api/action#enabled_state
765+
*/
766+
defaultState?: PerBrowserOption<'enabled' | 'disabled'>;
761767
browserStyle?: PerBrowserOption<boolean>;
762768
/**
763769
* Firefox only. Defines the part of the browser in which the button is

0 commit comments

Comments
 (0)