-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
ref(core): MediaQueryList module improvements #10947
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
CatchABus
wants to merge
12
commits into
NativeScript:main
Choose a base branch
from
CatchABus:ref/media-query-list
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
cc8a658
ref(core): MediaQueryList module improvements
CatchABus a39828b
ref: Better access to application events
CatchABus 13a2552
chore: Minor declaration order change
CatchABus 3431802
chore: Removed error throws from MediaQueryList methods
CatchABus e7a9387
chore: Event name type correction
CatchABus 80d6d13
chore: Added a precaution check
CatchABus 38c5a0a
ref: Type improvements
CatchABus b80fe86
chore: json representation of media query list
CatchABus bcec3b3
chore: type name correction
CatchABus 5015566
chore: renamed class properties
CatchABus 2b827f6
ref: css-mediaquery module refactoring
CatchABus 2f28a28
chore: Added media query list export
CatchABus File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,23 @@ | ||
| export const ApplicationEventNames = Object.freeze({ | ||
| launchEvent: 'launch', | ||
| suspendEvent: 'suspend', | ||
| displayedEvent: 'displayed', | ||
| backgroundEvent: 'background', | ||
| foregroundEvent: 'foreground', | ||
| resumeEvent: 'resume', | ||
| exitEvent: 'exit', | ||
| lowMemoryEvent: 'lowMemory', | ||
| uncaughtErrorEvent: 'uncaughtError', | ||
| discardedErrorEvent: 'discardedError', | ||
| orientationChangedEvent: 'orientationChanged', | ||
| systemAppearanceChangedEvent: 'systemAppearanceChanged', | ||
| layoutDirectionChangedEvent: 'layoutDirectionChanged', | ||
| fontScaleChangedEvent: 'fontScaleChanged', | ||
| livesyncEvent: 'livesync', | ||
| loadAppCssEvent: 'loadAppCss', | ||
| cssChangedEvent: 'cssChanged', | ||
| initRootViewEvent: 'initRootView', | ||
| }); | ||
|
|
||
| export type ApplicationEventNameType = typeof ApplicationEventNames; | ||
| export type ApplicationEventName = ApplicationEventNameType[keyof ApplicationEventNameType]; | ||
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
| @@ -1,118 +1,16 @@ | ||
| import { MediaQueryType, matchQuery, parseQuery } from '.'; | ||
| import { checkIfMediaQueryMatches } from '.'; | ||
| import { Screen } from '../platform'; | ||
|
|
||
| describe('css-mediaquery', () => { | ||
| describe('parseQuery', () => { | ||
| it('should parse media queries without expressions', () => { | ||
| expect(parseQuery('screen')).toEqual([ | ||
| { | ||
| inverse: false, | ||
| type: MediaQueryType.screen, | ||
| features: [], | ||
| }, | ||
| ]); | ||
| const { widthDIPs } = Screen.mainScreen; | ||
|
|
||
| expect(parseQuery('not screen')).toEqual([ | ||
| { | ||
| inverse: true, | ||
| type: MediaQueryType.screen, | ||
| features: [], | ||
| }, | ||
| ]); | ||
| describe('checkIfMediaQueryMatches', () => { | ||
| it('should return true for a correct match', () => { | ||
| expect(checkIfMediaQueryMatches(`only screen and (max-width: ${widthDIPs})`)).toBe(true); | ||
| }); | ||
|
|
||
| it('should throw a SyntaxError when a media query is invalid', () => { | ||
| expect(() => parseQuery('some crap')).toThrow(SyntaxError); | ||
| expect(() => parseQuery('48em')).toThrow(SyntaxError); | ||
| expect(() => parseQuery('screen and crap')).toThrow(SyntaxError); | ||
| expect(() => parseQuery('screen and (48em)')).toThrow(SyntaxError); | ||
| expect(() => parseQuery('screen and (foo:)')).toThrow(SyntaxError); | ||
| expect(() => parseQuery('()')).toThrow(SyntaxError); | ||
| expect(() => parseQuery('(foo) (bar)')).toThrow(SyntaxError); | ||
| expect(() => parseQuery('(foo:) and (bar)')).toThrow(SyntaxError); | ||
| }); | ||
| }); | ||
|
|
||
| describe('matchQuery', () => { | ||
| describe('Equality check', () => { | ||
| it('orientation: should return true for a correct match (===)', () => { | ||
| expect(matchQuery('(orientation: portrait)', { orientation: 'portrait' })).toBe(true); | ||
| }); | ||
|
|
||
| it('orientation: should return false for an incorrect match (===)', () => { | ||
| expect(matchQuery('(orientation: landscape)', { orientation: 'portrait' })).toBe(false); | ||
| }); | ||
|
|
||
| it('prefers-color-scheme: should return true for a correct match (===)', () => { | ||
| expect(matchQuery('(prefers-color-scheme: dark)', { 'prefers-color-scheme': 'dark' })).toBe(true); | ||
| }); | ||
|
|
||
| it('prefers-color-scheme: should return false for an incorrect match (===)', () => { | ||
| expect(matchQuery('(prefers-color-scheme: light)', { 'prefers-color-scheme': 'dark' })).toBe(false); | ||
| }); | ||
|
|
||
| it('width: should return true for a correct match', () => { | ||
| expect(matchQuery('(width: 800px)', { width: 800 })).toBe(true); | ||
| }); | ||
|
|
||
| it('width: should return false for an incorrect match', () => { | ||
| expect(matchQuery('(width: 800px)', { width: 900 })).toBe(false); | ||
| }); | ||
| }); | ||
|
|
||
| describe('Type', () => { | ||
| it('should return true for a correct match', () => { | ||
| expect(matchQuery('screen', { type: MediaQueryType.screen })).toBe(true); | ||
| }); | ||
|
|
||
| it('should return false for an incorrect match', () => { | ||
| expect( | ||
| matchQuery('screen and (orientation: portrait)', { | ||
| type: MediaQueryType.print, | ||
| orientation: 'portrait', | ||
| }), | ||
| ).toBe(false); | ||
| }); | ||
|
|
||
| it('should return false for a media query without a type when type is specified in the value object', () => { | ||
| expect(matchQuery('(min-width: 500px)', { type: MediaQueryType.screen })).toBe(false); | ||
| }); | ||
|
|
||
| it('should return true for a media query without a type when type is not specified in the value object', () => { | ||
| expect(matchQuery('(min-width: 500px)', { width: 700 })).toBe(true); | ||
| }); | ||
| }); | ||
|
|
||
| describe('Not', () => { | ||
| it('should return false when theres a match on a `not` query', () => { | ||
| expect( | ||
| matchQuery('not screen and (orientation: portrait)', { | ||
| type: MediaQueryType.screen, | ||
| orientation: 'landscape', | ||
| }), | ||
| ).toBe(false); | ||
| }); | ||
|
|
||
| it('should not disrupt an OR query', () => { | ||
| expect( | ||
| matchQuery('not screen and (color), screen and (min-height: 48em)', { | ||
| type: MediaQueryType.screen, | ||
| height: 1000, | ||
| }), | ||
| ).toBe(true); | ||
| }); | ||
|
|
||
| it('should return false for when type === all', () => { | ||
| expect( | ||
| matchQuery('not all and (min-width: 48em)', { | ||
| type: MediaQueryType.all, | ||
| width: 1000, | ||
| }), | ||
| ).toBe(false); | ||
| }); | ||
|
|
||
| it('should return true for inverted value', () => { | ||
| expect(matchQuery('not screen and (min-width: 48px)', { width: 24 })).toBe(true); | ||
| }); | ||
| it('should return false for an incorrect match', () => { | ||
| expect(checkIfMediaQueryMatches(`only screen and (max-width: ${widthDIPs - 1})`)).toBe(false); | ||
| }); | ||
| }); | ||
| }); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@NathanWalker I forgot to mention this but I put application event names into their own separate module and
Applicationinstance accesses them using getters.At the same time, Application implements the type of the ApplicationEventNames to ensure that no getter is accidentally missed.
If this change looks ugly, we can definitely revert it.