-
Notifications
You must be signed in to change notification settings - Fork 162
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Introduce flag to remove high-contrast header (#1883)
Co-authored-by: Francesco Longo <[email protected]>
- Loading branch information
Showing
20 changed files
with
165 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
import * as globalFlags from '../global-flags'; | ||
const { getGlobalFlag } = globalFlags; | ||
|
||
const awsuiGlobalFlagsSymbol = Symbol.for('awsui-global-flags'); | ||
interface GlobalFlags { | ||
removeHighContrastHeader?: boolean; | ||
} | ||
|
||
interface ExtendedWindow extends Window { | ||
[awsuiGlobalFlagsSymbol]?: GlobalFlags; | ||
} | ||
declare const window: ExtendedWindow; | ||
|
||
afterEach(() => { | ||
delete window[awsuiGlobalFlagsSymbol]; | ||
jest.restoreAllMocks(); | ||
}); | ||
|
||
describe('getGlobalFlag', () => { | ||
test('returns undefined if there global flags are not defined', () => { | ||
expect(getGlobalFlag('removeHighContrastHeader')).toBeUndefined(); | ||
}); | ||
test('returns undefined if global flags are defined but flag is not set', () => { | ||
window[awsuiGlobalFlagsSymbol] = {}; | ||
expect(getGlobalFlag('removeHighContrastHeader')).toBeUndefined(); | ||
}); | ||
test('returns removeHighContrastHeader value', () => { | ||
window[awsuiGlobalFlagsSymbol] = { removeHighContrastHeader: false }; | ||
expect(getGlobalFlag('removeHighContrastHeader')).toBe(false); | ||
window[awsuiGlobalFlagsSymbol].removeHighContrastHeader = true; | ||
expect(getGlobalFlag('removeHighContrastHeader')).toBe(true); | ||
}); | ||
test('returns removeHighContrastHeader value when defined in top window', () => { | ||
jest | ||
.spyOn(globalFlags, 'getTopWindow') | ||
.mockReturnValue({ [awsuiGlobalFlagsSymbol]: { removeHighContrastHeader: true } } as unknown as ExtendedWindow); | ||
expect(getGlobalFlag('removeHighContrastHeader')).toBe(true); | ||
jest.restoreAllMocks(); | ||
|
||
jest | ||
.spyOn(globalFlags, 'getTopWindow') | ||
.mockReturnValue({ [awsuiGlobalFlagsSymbol]: { removeHighContrastHeader: false } } as unknown as ExtendedWindow); | ||
expect(getGlobalFlag('removeHighContrastHeader')).toBe(false); | ||
}); | ||
test('privileges values in the self window', () => { | ||
jest | ||
.spyOn(globalFlags, 'getTopWindow') | ||
.mockReturnValue({ [awsuiGlobalFlagsSymbol]: { removeHighContrastHeader: false } } as unknown as ExtendedWindow); | ||
window[awsuiGlobalFlagsSymbol] = { removeHighContrastHeader: true }; | ||
expect(getGlobalFlag('removeHighContrastHeader')).toBe(true); | ||
}); | ||
test('returns top window value when not defined in the self window', () => { | ||
jest | ||
.spyOn(globalFlags, 'getTopWindow') | ||
.mockReturnValue({ [awsuiGlobalFlagsSymbol]: { removeHighContrastHeader: true } } as unknown as ExtendedWindow); | ||
window[awsuiGlobalFlagsSymbol] = {}; | ||
expect(getGlobalFlag('removeHighContrastHeader')).toBe(true); | ||
}); | ||
test('returns undefined when top window is undefined', () => { | ||
jest.spyOn(globalFlags, 'getTopWindow').mockReturnValue(undefined); | ||
expect(getGlobalFlag('removeHighContrastHeader')).toBeUndefined(); | ||
}); | ||
test('returns undefined when an error is thrown and flag is not defined in own window', () => { | ||
jest.spyOn(globalFlags, 'getTopWindow').mockImplementation(() => { | ||
throw new Error('whatever'); | ||
}); | ||
expect(getGlobalFlag('removeHighContrastHeader')).toBeUndefined(); | ||
}); | ||
test('returns value when an error is thrown and flag is defined in own window', () => { | ||
jest.spyOn(globalFlags, 'getTopWindow').mockImplementation(() => { | ||
throw new Error('whatever'); | ||
}); | ||
window[awsuiGlobalFlagsSymbol] = { removeHighContrastHeader: true }; | ||
expect(getGlobalFlag('removeHighContrastHeader')).toBe(true); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
import { getGlobalFlag } from './global-flags'; | ||
|
||
export const contentHeaderClassName: string = getGlobalFlag('removeHighContrastHeader') | ||
? '' | ||
: 'awsui-context-content-header'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
export const awsuiGlobalFlagsSymbol = Symbol.for('awsui-global-flags'); | ||
|
||
interface GlobalFlags { | ||
removeHighContrastHeader?: boolean; | ||
} | ||
|
||
interface ExtendedWindow extends Window { | ||
[awsuiGlobalFlagsSymbol]?: GlobalFlags; | ||
} | ||
declare const window: ExtendedWindow; | ||
|
||
export const getTopWindow = (): ExtendedWindow | undefined => { | ||
return window.top as ExtendedWindow; | ||
}; | ||
|
||
function readFlag(window: ExtendedWindow | undefined, flagName: keyof GlobalFlags) { | ||
if (typeof window === 'undefined' || !window[awsuiGlobalFlagsSymbol]) { | ||
return undefined; | ||
} | ||
return window[awsuiGlobalFlagsSymbol][flagName]; | ||
} | ||
|
||
export const getGlobalFlag = (flagName: keyof GlobalFlags): GlobalFlags[keyof GlobalFlags] | undefined => { | ||
try { | ||
const ownFlag = readFlag(window, flagName); | ||
if (ownFlag !== undefined) { | ||
return ownFlag; | ||
} | ||
return readFlag(getTopWindow(), flagName); | ||
} catch (e) { | ||
return undefined; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.