-
Notifications
You must be signed in to change notification settings - Fork 233
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #232 from Telegram-Mini-Apps/feature/css-utils
Export CSS utilities
- Loading branch information
Showing
12 changed files
with
326 additions
and
14 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@tma.js/sdk": patch | ||
--- | ||
|
||
Move css utils to the separate folder. Write tests. Fix a bug in viewport stable height CSS var |
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,175 @@ | ||
import { expect, vi, it, SpyInstance, afterEach, beforeAll, describe } from 'vitest'; | ||
import { ThemeParams } from '../../theme-params'; | ||
import { dispatchWindowMessageEvent } from '../../../test-utils/dispatchWindowMessageEvent'; | ||
import { bindMiniAppCSSVars } from '../bindMiniAppCSSVars'; | ||
import { MiniApp } from '../../mini-app'; | ||
|
||
let setCSSPropertySpy: SpyInstance<[string, string, string?], void>; | ||
|
||
beforeAll(() => { | ||
setCSSPropertySpy = vi | ||
.spyOn(document.documentElement.style, 'setProperty') | ||
.mockImplementation(() => { | ||
}); | ||
}); | ||
|
||
afterEach(() => { | ||
vi.clearAllMocks(); | ||
}); | ||
|
||
describe('background', () => { | ||
it('should set --tg-background-color equal to miniApp.backgroundColor', () => { | ||
bindMiniAppCSSVars( | ||
new MiniApp({ | ||
backgroundColor: '#111111', | ||
headerColor: '#222222', | ||
botInline: false, | ||
version: '7.0', | ||
createRequestId() { | ||
return 'abc'; | ||
}, | ||
}), | ||
new ThemeParams({}), | ||
); | ||
|
||
expect(setCSSPropertySpy).toHaveBeenCalledWith('--tg-background-color', '#111111'); | ||
}); | ||
|
||
it('should update --tg-background-color according to the changes applied to mini app background color', () => { | ||
const ma = new MiniApp({ | ||
backgroundColor: '#111111', | ||
headerColor: '#222222', | ||
botInline: false, | ||
version: '7.0', | ||
postEvent() { | ||
}, | ||
createRequestId() { | ||
return 'abc'; | ||
}, | ||
}); | ||
|
||
bindMiniAppCSSVars(ma, new ThemeParams({})); | ||
setCSSPropertySpy.mockClear(); | ||
|
||
ma.setBackgroundColor('#999999'); | ||
expect(setCSSPropertySpy).toHaveBeenCalledWith('--tg-background-color', '#999999'); | ||
}); | ||
}); | ||
|
||
describe('header', () => { | ||
it('should set --tg-header-color equal to miniApp.headerColor if this value is RGB', () => { | ||
bindMiniAppCSSVars( | ||
new MiniApp({ | ||
backgroundColor: '#111111', | ||
headerColor: '#222222', | ||
botInline: false, | ||
version: '7.0', | ||
createRequestId() { | ||
return 'abc'; | ||
}, | ||
}), | ||
new ThemeParams({}), | ||
); | ||
|
||
expect(setCSSPropertySpy).toHaveBeenCalledWith('--tg-header-color', '#222222'); | ||
}); | ||
|
||
it('should set --tg-header-color equal to themeParams.backgroundColor if miniApp.headerColor = "bg_color" and backgroundColor property is presented in theme params', () => { | ||
const tp = new ThemeParams({}); | ||
tp.listen(); | ||
|
||
bindMiniAppCSSVars( | ||
new MiniApp({ | ||
backgroundColor: '#111111', | ||
headerColor: 'bg_color', | ||
botInline: false, | ||
version: '7.0', | ||
createRequestId() { | ||
return 'abc'; | ||
}, | ||
}), | ||
tp, | ||
); | ||
|
||
// Background only. | ||
expect(setCSSPropertySpy).toHaveBeenCalledTimes(1); | ||
setCSSPropertySpy.mockClear(); | ||
|
||
dispatchWindowMessageEvent('theme_changed', { | ||
theme_params: { | ||
bg_color: '#ffffff', | ||
}, | ||
}); | ||
|
||
expect(setCSSPropertySpy).toHaveBeenCalledTimes(1); | ||
expect(setCSSPropertySpy).toHaveBeenCalledWith('--tg-header-color', '#ffffff'); | ||
}); | ||
|
||
it('should set --tg-header-color equal to themeParams.secondaryBackgroundColor if miniApp.headerColor = "secondary_bg_color" and secondaryBackgroundColor property is presented in theme params', () => { | ||
const tp = new ThemeParams({}); | ||
tp.listen(); | ||
|
||
bindMiniAppCSSVars( | ||
new MiniApp({ | ||
backgroundColor: '#111111', | ||
headerColor: 'secondary_bg_color', | ||
botInline: false, | ||
version: '7.0', | ||
createRequestId() { | ||
return 'abc'; | ||
}, | ||
}), | ||
tp, | ||
); | ||
|
||
// Background only. | ||
expect(setCSSPropertySpy).toHaveBeenCalledTimes(1); | ||
setCSSPropertySpy.mockClear(); | ||
|
||
dispatchWindowMessageEvent('theme_changed', { | ||
theme_params: { | ||
secondary_bg_color: '#000000', | ||
}, | ||
}); | ||
expect(setCSSPropertySpy).toHaveBeenCalledWith('--tg-header-color', '#000000'); | ||
}); | ||
|
||
it('should actualize --tg-header-color if either mini app or theme params changed', () => { | ||
const ma = new MiniApp({ | ||
backgroundColor: '#111111', | ||
headerColor: '#aabbcc', | ||
botInline: false, | ||
version: '7.0', | ||
postEvent() {}, | ||
createRequestId() { | ||
return 'abc'; | ||
}, | ||
}); | ||
const tp = new ThemeParams({ | ||
backgroundColor: '#ffffff', | ||
secondaryBackgroundColor: '#000000', | ||
}); | ||
tp.listen(); | ||
|
||
bindMiniAppCSSVars(ma, tp); | ||
setCSSPropertySpy.mockClear(); | ||
|
||
ma.setHeaderColor('bg_color'); | ||
expect(setCSSPropertySpy).toHaveBeenCalledTimes(1); | ||
expect(setCSSPropertySpy).toHaveBeenCalledWith('--tg-header-color', '#ffffff'); | ||
setCSSPropertySpy.mockClear(); | ||
|
||
ma.setHeaderColor('secondary_bg_color'); | ||
expect(setCSSPropertySpy).toHaveBeenCalledTimes(1); | ||
expect(setCSSPropertySpy).toHaveBeenCalledWith('--tg-header-color', '#000000'); | ||
setCSSPropertySpy.mockClear(); | ||
|
||
dispatchWindowMessageEvent('theme_changed', { | ||
theme_params: { | ||
secondary_bg_color: '#abcdef', | ||
}, | ||
}); | ||
expect(setCSSPropertySpy).toHaveBeenCalledWith('--tg-header-color', '#abcdef'); | ||
setCSSPropertySpy.mockClear(); | ||
}); | ||
}); |
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,52 @@ | ||
import { expect, vi, it, SpyInstance, afterEach, beforeAll } from 'vitest'; | ||
import { bindThemeCSSVars } from '../bindThemeCSSVars'; | ||
import { ThemeParams } from '../../theme-params'; | ||
import { dispatchWindowMessageEvent } from '../../../test-utils/dispatchWindowMessageEvent'; | ||
|
||
let setCSSPropertySpy: SpyInstance<[string, string, string?], void>; | ||
|
||
beforeAll(() => { | ||
setCSSPropertySpy = vi | ||
.spyOn(document.documentElement.style, 'setProperty') | ||
.mockImplementation(() => { | ||
}); | ||
}); | ||
|
||
afterEach(() => { | ||
vi.clearAllMocks(); | ||
}); | ||
|
||
it('should set --tg-theme-{key} CSS vars, where key is a kebab-cased theme keys', () => { | ||
bindThemeCSSVars(new ThemeParams({ | ||
backgroundColor: '#abcdef', | ||
accentTextColor: '#000011', | ||
})); | ||
|
||
expect(setCSSPropertySpy).toHaveBeenCalledTimes(2); | ||
expect(setCSSPropertySpy).toHaveBeenCalledWith('--tg-theme-background-color', '#abcdef'); | ||
expect(setCSSPropertySpy).toHaveBeenCalledWith('--tg-theme-accent-text-color', '#000011'); | ||
}); | ||
|
||
it('should update --tg-theme-{key}variables to the values, received in the Theme change events', async () => { | ||
const tp = new ThemeParams({ | ||
backgroundColor: '#abcdef', | ||
accentTextColor: '#000011', | ||
}); | ||
bindThemeCSSVars(tp); | ||
setCSSPropertySpy.mockClear(); | ||
|
||
await tp.listen(); | ||
|
||
dispatchWindowMessageEvent('theme_changed', { | ||
theme_params: { | ||
bg_color: '#111111', | ||
accent_text_color: '#222222', | ||
text_color: '#333333', | ||
}, | ||
}); | ||
|
||
expect(setCSSPropertySpy).toHaveBeenCalledTimes(3); | ||
expect(setCSSPropertySpy).toHaveBeenCalledWith('--tg-theme-background-color', '#111111'); | ||
expect(setCSSPropertySpy).toHaveBeenCalledWith('--tg-theme-accent-text-color', '#222222'); | ||
expect(setCSSPropertySpy).toHaveBeenCalledWith('--tg-theme-text-color', '#333333'); | ||
}); |
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,55 @@ | ||
import { expect, vi, it, SpyInstance, afterEach, beforeAll } from 'vitest'; | ||
import { bindViewportCSSVars } from '../bindViewportCSSVars'; | ||
import { Viewport } from '../../viewport'; | ||
import { dispatchWindowMessageEvent } from '../../../test-utils/dispatchWindowMessageEvent'; | ||
|
||
let setCSSPropertySpy: SpyInstance<[string, string, string?], void>; | ||
|
||
beforeAll(() => { | ||
setCSSPropertySpy = vi | ||
.spyOn(document.documentElement.style, 'setProperty') | ||
.mockImplementation(() => { | ||
}); | ||
}); | ||
|
||
afterEach(() => { | ||
vi.clearAllMocks(); | ||
}); | ||
|
||
it('should set --tg-viewport-height = viewport.height, --tg-viewport-width = viewport.width, --tg-viewport-stable-height = viewport.stableHeight', () => { | ||
bindViewportCSSVars(new Viewport({ | ||
height: 192, | ||
width: 1000, | ||
isExpanded: false, | ||
stableHeight: 200, | ||
})); | ||
|
||
expect(setCSSPropertySpy).toHaveBeenCalledTimes(3); | ||
expect(setCSSPropertySpy).toHaveBeenCalledWith('--tg-viewport-height', '192px'); | ||
expect(setCSSPropertySpy).toHaveBeenCalledWith('--tg-viewport-width', '1000px'); | ||
expect(setCSSPropertySpy).toHaveBeenCalledWith('--tg-viewport-stable-height', '200px'); | ||
}); | ||
|
||
it('should update --tg-viewport-height, --tg-viewport-width, --tg-viewport-stable-height according to the values, received in the Viewport change events', () => { | ||
const viewport = new Viewport({ | ||
height: 192, | ||
width: 1000, | ||
isExpanded: false, | ||
stableHeight: 200, | ||
}); | ||
bindViewportCSSVars(viewport); | ||
setCSSPropertySpy.mockClear(); | ||
|
||
viewport.listen(); | ||
dispatchWindowMessageEvent('viewport_changed', { | ||
height: 900, | ||
is_state_stable: true, | ||
is_expanded: false, | ||
width: 1800, | ||
}) | ||
|
||
expect(setCSSPropertySpy).toHaveBeenCalledTimes(3); | ||
expect(setCSSPropertySpy).toHaveBeenCalledWith('--tg-viewport-height', '900px'); | ||
expect(setCSSPropertySpy).toHaveBeenCalledWith('--tg-viewport-width', '1800px'); | ||
expect(setCSSPropertySpy).toHaveBeenCalledWith('--tg-viewport-stable-height', '900px'); | ||
}); |
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,14 @@ | ||
import { afterEach, expect, it, vi } from 'vitest'; | ||
import { setCSSVar } from '../setCSSVar'; | ||
|
||
afterEach(() => { | ||
vi.restoreAllMocks(); | ||
}) | ||
|
||
it('should call document.documentElement.style.setProperty with passed "name" and "value" arguments', () => { | ||
const spy = vi.spyOn(document.documentElement.style, 'setProperty').mockImplementation(() => {}); | ||
|
||
setCSSVar('tma', 'is cool') | ||
expect(spy).toHaveBeenCalledOnce(); | ||
expect(spy).toHaveBeenCalledWith('tma', 'is cool'); | ||
}) |
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
File renamed without changes.
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,4 @@ | ||
export * from './bindMiniAppCSSVars.js'; | ||
export * from './bindThemeCSSVars.js'; | ||
export * from './bindViewportCSSVars.js'; | ||
export * from './setCSSVar.js'; |
File renamed without changes.
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