Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion extension.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@
"ext.aggrid": {
"packageFiles": [
"ext.aggrid/init.js",
"ext.aggrid/mountGrid.js"
"ext.aggrid/mountGrid.js",
"ext.aggrid/theme.js"
],
"styles": [
"ext.aggrid/ext.aggrid.less"
Expand Down
6 changes: 6 additions & 0 deletions modules/ext.aggrid/mountGrid.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
/* global agGrid */

const { getWikiTheme } = require( './theme.js' );

const PLACEHOLDER_SELECTOR = '.ext-aggrid';
const CONFIG_ATTR = 'data-mw-aggrid-options';
// Marks a placeholder as already mounted so re-runs (e.g. wikipage.content
Expand Down Expand Up @@ -38,6 +40,10 @@ function mountGrid( el ) {
if ( !gridOptions ) {
return;
}
// Apply the wiki theme unless the author already chose one.
if ( !gridOptions.theme ) {
gridOptions.theme = getWikiTheme();
}
// Mark before mounting so a concurrent/re-entrant pass can't double-mount.
el.classList.add( INIT_CLASS );
// agGrid is the global exposed by the vendored AG Grid bundle.
Expand Down
40 changes: 40 additions & 0 deletions modules/ext.aggrid/theme.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/* global agGrid */

// AG Grid theme colour params mapped to Codex design tokens (CSS variables).
// The grid follows the wiki light/dark/OS scheme and skin colour
// customisations through the cascade — no JS theme-syncing. Fallbacks are the
// REL1_43 light token values, so skins without Codex tokens still render a
// usable light grid. Typography inherits the page font; AG Grid's concrete
// fontSize/spacing defaults are kept (it needs them for row-height layout).
const THEME_PARAMS = {
backgroundColor: 'var(--background-color-base, #fff)',
foregroundColor: 'var(--color-base, #202122)',
borderColor: 'var(--border-color-base, #a2a9b1)',
chromeBackgroundColor: 'var(--background-color-neutral-subtle, #f8f9fa)',
headerBackgroundColor: 'var(--background-color-neutral-subtle, #f8f9fa)',
headerTextColor: 'var(--color-base, #202122)',
accentColor: 'var(--color-progressive, #36c)',
rowHoverColor: 'var(--background-color-interactive-subtle, #f8f9fa)',
selectedRowBackgroundColor: 'var(--background-color-progressive-subtle, #f1f4fd)',
fontFamily: 'inherit',
// Inherit the page color-scheme rather than AG Grid's default of "light", so
// the wrapper doesn't pin itself to light and light-dark() color tokens
// resolve to the page's current mode inside the grid.
browserColorScheme: 'inherit'
};

let cachedTheme = null;

/**
* The shared AG Grid theme, built once from themeQuartz.
*
* @return {Object} An AG Grid Theme object.
*/
function getWikiTheme() {
if ( !cachedTheme ) {
cachedTheme = agGrid.themeQuartz.withParams( THEME_PARAMS );
}
return cachedTheme;
}

module.exports = { THEME_PARAMS: THEME_PARAMS, getWikiTheme: getWikiTheme };
19 changes: 18 additions & 1 deletion tests/vitest/ext.aggrid/mountGrid.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,10 @@ describe( 'parseConfig', () => {

describe( 'mountGrid', () => {
beforeEach( () => {
global.agGrid = { createGrid: vi.fn() };
global.agGrid = {
createGrid: vi.fn(),
themeQuartz: { withParams: () => ( { __wiki: true } ) }
};
} );

afterEach( () => {
Expand All @@ -57,4 +60,18 @@ describe( 'mountGrid', () => {
expect( global.agGrid.createGrid ).not.toHaveBeenCalled();
expect( el.classList.contains( 'ext-aggrid--init' ) ).toBe( false );
} );

it( 'applies the wiki theme when the config has none', () => {
const el = makeEl( '{"columnDefs":[],"rowData":[]}' );
mountGrid( el );
const opts = global.agGrid.createGrid.mock.calls[ 0 ][ 1 ];
expect( opts.theme ).toEqual( { __wiki: true } );
} );

it( 'preserves a theme already set in the config', () => {
const el = makeEl( '{"columnDefs":[],"rowData":[],"theme":"legacy"}' );
mountGrid( el );
const opts = global.agGrid.createGrid.mock.calls[ 0 ][ 1 ];
expect( opts.theme ).toBe( 'legacy' );
} );
} );
45 changes: 45 additions & 0 deletions tests/vitest/ext.aggrid/theme.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
const { THEME_PARAMS, getWikiTheme } = require( '../../../modules/ext.aggrid/theme.js' );

describe( 'THEME_PARAMS', () => {
const expected = {
backgroundColor: 'var(--background-color-base, #fff)',
foregroundColor: 'var(--color-base, #202122)',
borderColor: 'var(--border-color-base, #a2a9b1)',
chromeBackgroundColor: 'var(--background-color-neutral-subtle, #f8f9fa)',
headerBackgroundColor: 'var(--background-color-neutral-subtle, #f8f9fa)',
headerTextColor: 'var(--color-base, #202122)',
accentColor: 'var(--color-progressive, #36c)',
rowHoverColor: 'var(--background-color-interactive-subtle, #f8f9fa)',
selectedRowBackgroundColor: 'var(--background-color-progressive-subtle, #f1f4fd)'
};

it( 'maps every colour param to a Codex token with a fallback', () => {
Object.keys( expected ).forEach( ( key ) => {
expect( THEME_PARAMS[ key ] ).toBe( expected[ key ] );
} );
} );

it( 'inherits typography rather than setting a font', () => {
expect( THEME_PARAMS.fontFamily ).toBe( 'inherit' );
expect( THEME_PARAMS.fontSize ).toBeUndefined();
} );

it( 'inherits the browser color-scheme so light-dark() tokens resolve', () => {
expect( THEME_PARAMS.browserColorScheme ).toBe( 'inherit' );
} );
} );

describe( 'getWikiTheme', () => {
it( 'builds the theme from themeQuartz.withParams and memoises it', () => {
const built = { id: 'wiki-theme' };
const withParams = vi.fn( () => built );
global.agGrid = { themeQuartz: { withParams } };

expect( getWikiTheme() ).toBe( built );
expect( getWikiTheme() ).toBe( built );
expect( withParams ).toHaveBeenCalledTimes( 1 );
expect( withParams ).toHaveBeenCalledWith( THEME_PARAMS );

delete global.agGrid;
} );
} );
Loading