Skip to content
This repository has been archived by the owner on Mar 4, 2020. It is now read-only.

fix: refactor approach for px-to-rem conversions #371

Merged
merged 16 commits into from
Dec 6, 2018
Merged
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm

### Fixes
- Fix `Provider` is not executing staticStyles with the merged siteVariables @mnajdova ([#559](https://github.com/stardust-ui/react/pull/559))
- Decouple `pxToRem` from HTML page's font size @kuzhelov ([#371](https://github.com/stardust-ui/react/pull/371))

### Features
- `Ref` components uses `forwardRef` API by default @layershifter ([#491](https://github.com/stardust-ui/react/pull/491))
Expand Down
12 changes: 2 additions & 10 deletions docs/src/app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { semanticCssOverrides } from './Style'
import { ThemeContext } from './context/theme-context'
import Router from './routes'

const semanticStyleOverrides = {
const semanticStylesOverrideTheme = {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

staticStyles: [semanticCssOverrides],
}

Expand Down Expand Up @@ -38,15 +38,7 @@ class App extends React.Component<any, AppState> {
const { themeName } = this.state
return (
<ThemeContext.Provider value={this.state}>
<Provider
theme={mergeThemes(semanticStyleOverrides, themes[themeName], {
// adjust Teams' theme to Semantic UI's font size scheme
siteVariables: {
htmlFontSize: '14px',
Copy link
Contributor Author

@kuzhelov kuzhelov Dec 6, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this now should be defined as <html> font size in docs' top-level index.ejs - to eliminate screen test regressions

bodyFontSize: '1rem',
},
})}
>
<Provider theme={mergeThemes(semanticStylesOverrideTheme, themes[themeName])}>
<Router />
</Provider>
</ThemeContext.Provider>
Expand Down
2 changes: 1 addition & 1 deletion docs/src/index.ejs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<!DOCTYPE html>
<html lang="en">
<html lang="en" style="font-size: 14px;">
<head>
<base href='<%= __BASENAME__ %>'>
<meta charset="UTF-8">
Expand Down
70 changes: 10 additions & 60 deletions src/lib/fontSizeUtility.ts
Original file line number Diff line number Diff line change
@@ -1,73 +1,23 @@
import * as _ from 'lodash'
import isBrowser from './isBrowser'

const DEFAULT_FONT_SIZE_IN_PX = 16
const DEFAULT_REM_SIZE_IN_PX = 10
let _htmlFontSizeInPx: number | null = null

const getComputedFontSize = (): number => {
return isBrowser()
? getFontSizeValue(getComputedStyle(document.documentElement).fontSize) ||
DEFAULT_REM_SIZE_IN_PX
: DEFAULT_FONT_SIZE_IN_PX
}

const getFontSizeValue = (size?: string | null): number | null => {
return (size && parseFloat(size)) || null
}

/**
* Converts the provided px size to rem based on the default font size of 10px unless
* the HTML font size has been previously defined with setHTMLFontSize().
* Converts the provided px size to rem based on the default font size of 16px unless
* another base size is provided.
* @param {number} value The px value to convert to rem.
* @param {number} [baseSize] Base font size to use for rem convertion. Optional, 16px is used by default.
* @example
* // Returns '1rem'
* pxToRem(10)
* pxToRem(16)
*
* * // Returns '2rem'
* pxToRem(16, 8)
* @returns {string} The value converted to the rem.
*/
export const pxToRem = (value: number = 0): string => {
if (!_htmlFontSizeInPx) {
_htmlFontSizeInPx = getComputedFontSize()
}

if (process.env.NODE_ENV !== 'production') {
if (value < 0) {
throw new Error(`Invalid value of: '${value}'.`)
}
}
const convertedValueInRems = value / _htmlFontSizeInPx
export const pxToRem = (value: number, baseSize?: number): string => {
const baseFontSize = baseSize || DEFAULT_FONT_SIZE_IN_PX
const convertedValueInRems = value / baseFontSize

return `${_.round(convertedValueInRems, 4)}rem`
}

/**
* Sets the HTML font size for use for px to rem conversion.
* Providing null for fontSize will get the computed font size based on the document, or set it to DEFAULT_REM_SIZE_IN_PX.
* @param {string} [fontSize] The font size in px, to set as the HTML font size in the fontSizeUtility.
* @example
* // Sets the HTML font size to 10px.
* setHTMLFontSize('10px')
* @example
* // Sets the HTML font size based on document.fontSize.
* setHTMLFontSize()
*/
export const setHTMLFontSize = (fontSize?: string): void => {
if (!fontSize) {
throw new Error('fontSize is not defined')
}

const htmlFontSizeValue = getFontSizeValue(fontSize) || 0
const htmlFontSizeUnit = fontSize.replace(htmlFontSizeValue.toString(), '')

if (process.env.NODE_ENV !== 'production') {
if (htmlFontSizeValue <= 0) {
throw new Error(`Invalid htmlFontSizeValue of: '${htmlFontSizeValue}'.`)
}

if (htmlFontSizeUnit !== 'px') {
throw new Error(`Expected htmlFontSize to be in px, but got: '${htmlFontSizeUnit}'.`)
}
}

_htmlFontSizeInPx = htmlFontSizeValue || getComputedFontSize()
}
2 changes: 1 addition & 1 deletion src/lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export { default as typescriptUtils } from './typescriptUtils'
export { default as doesNodeContainClick } from './doesNodeContainClick'
export { default as leven } from './leven'

export { pxToRem, setHTMLFontSize } from './fontSizeUtility'
export { pxToRem } from './fontSizeUtility'
export { customPropTypes }
export { default as createAnimationStyles } from './createAnimationStyles'
export { default as createComponent } from './createStardustComponent'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { pxToRem } from '../../../../lib'
import { pxToRem } from '../../../teams/utils'

export default (siteVars: any) => {
return {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { ComponentSlotStylesInput, ICSSInJSStyle } from '../../../types'
import { AttachmentProps } from '../../../../components/Attachment/Attachment'
import { AttachmentVariables } from './attachmentVariables'
import { pxToRem } from '../../../../lib'
import { pxToRem } from '../../utils'

const attachmentStyles: ComponentSlotStylesInput<AttachmentProps, AttachmentVariables> = {
root: ({ props, variables }): ICSSInJSStyle => ({
Expand Down
2 changes: 1 addition & 1 deletion src/themes/teams/components/Avatar/avatarStyles.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { pxToRem } from '../../../../lib'
import { pxToRem } from '../../utils'
import { ComponentSlotStylesInput, ICSSInJSStyle } from '../../../types'
import { AvatarPropsWithDefaults } from '../../../../components/Avatar/Avatar'

Expand Down
2 changes: 1 addition & 1 deletion src/themes/teams/components/Button/buttonStyles.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { pxToRem } from '../../../../lib'
import { pxToRem } from '../../utils'
import { ComponentSlotStylesInput, ICSSInJSStyle } from '../../../types'
import { ButtonProps, ButtonState } from '../../../../components/Button/Button'
import { truncateStyle } from '../../../../styles/customCSS'
Expand Down
2 changes: 1 addition & 1 deletion src/themes/teams/components/Button/buttonVariables.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { pxToRem } from '../../../../lib'
import { pxToRem } from '../../utils'

export interface ButtonVariables {
[key: string]: string | number
Expand Down
2 changes: 1 addition & 1 deletion src/themes/teams/components/Chat/chatMessageStyles.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { ComponentSlotStylesInput, ICSSInJSStyle } from '../../../types'
import { ChatMessageProps } from '../../../../components/Chat/ChatMessage'
import { ChatMessageVariables } from './chatMessageVariables'
import { pxToRem } from '../../../../lib'
import { pxToRem } from '../../utils'

const px10asRem = pxToRem(10)

Expand Down
2 changes: 1 addition & 1 deletion src/themes/teams/components/Chat/chatStyles.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { ICSSInJSStyle } from '../../../types'
import { ChatVariables } from './chatVariables'
import { pxToRem } from '../../../../lib'
import { pxToRem } from '../../utils'

const chatStyles = {
root: ({ variables: v }: { variables: ChatVariables }): ICSSInJSStyle => ({
Expand Down
3 changes: 2 additions & 1 deletion src/themes/teams/components/Divider/dividerStyles.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import * as _ from 'lodash'

import { childrenExist, pxToRem } from '../../../../lib'
import { childrenExist } from '../../../../lib'
import { pxToRem } from '../../utils'
import { ComponentSlotStylesInput, ICSSInJSStyle, ICSSPseudoElementStyle } from '../../../types'
import { DividerPropsWithDefaults } from '../../../../components/Divider/Divider'

Expand Down
2 changes: 1 addition & 1 deletion src/themes/teams/components/Divider/dividerVariables.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as _ from 'lodash'
import { pxToRem } from '../../utils'

import { EmphasisColors, NaturalColors } from '../../../types'
import { pxToRem } from '../../../../lib'

export interface DividerVariables {
colors: Record<keyof (EmphasisColors & NaturalColors), string>
Expand Down
2 changes: 1 addition & 1 deletion src/themes/teams/components/Form/formFieldStyles.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { ComponentSlotStylesInput, ICSSInJSStyle } from '../../../types'
import { FormProps } from '../../../../components/Form/Form'
import { pxToRem } from '../../../../lib'
import { pxToRem } from '../../utils'

const formFieldStyles: ComponentSlotStylesInput<FormProps, any> = {
root: ({ props, variables }): ICSSInJSStyle => ({}),
Expand Down
2 changes: 1 addition & 1 deletion src/themes/teams/components/Form/formStyles.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { ComponentSlotStylesInput, ICSSInJSStyle } from '../../../types'
import { FormProps } from '../../../../components/Form/Form'
import { pxToRem } from '../../../../lib'
import { pxToRem } from '../../utils'

const formStyles: ComponentSlotStylesInput<FormProps, any> = {
root: ({ props, variables }): ICSSInJSStyle => ({
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { pxToRem } from '../../../../lib'
import { pxToRem } from '../../utils'
import { ICSSInJSStyle } from '../../../types'

export default {
Expand Down
2 changes: 1 addition & 1 deletion src/themes/teams/components/Icon/iconStyles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { fittedStyle } from '../../../../styles/customCSS'
import { ComponentSlotStylesInput, ICSSInJSStyle, FontIconSpec } from '../../../types'
import { ResultOf } from '../../../../../types/utils'
import { IconXSpacing, IconProps } from '../../../../components/Icon/Icon'
import { pxToRem } from './../../../../lib'
import { pxToRem } from '../../utils'
import { getStyle as getSvgStyle } from './svg'

const sizes = new Map([
Expand Down
2 changes: 1 addition & 1 deletion src/themes/teams/components/Icon/iconVariables.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { pxToRem } from '../../../../lib'
import { pxToRem } from '../../utils'

export interface IconVariables {
[key: string]: string | number | boolean | undefined
Expand Down
2 changes: 1 addition & 1 deletion src/themes/teams/components/Image/imageStyles.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { pxToRem } from '../../../../lib'
import { pxToRem } from '../../utils'
import { ComponentSlotStylesInput, ICSSInJSStyle } from '../../../types'
import { ImageProps } from '../../../../components/Image/Image'

Expand Down
2 changes: 1 addition & 1 deletion src/themes/teams/components/Image/imageVariables.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { pxToRem } from '../../../../lib'
import { pxToRem } from '../../utils'

export default () => ({
width: undefined,
Expand Down
2 changes: 1 addition & 1 deletion src/themes/teams/components/Input/inputVariables.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { pxToRem } from '../../../../lib'
import { pxToRem } from '../../utils'

export interface InputVariables {
backgroundColor: string
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { pxToRem } from '../../../../lib'
import { pxToRem } from '../../utils'

export default () => {
const vars: any = {}
Expand Down
2 changes: 1 addition & 1 deletion src/themes/teams/components/Label/labelStyles.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { pxToRem } from '../../../../lib'
import { pxToRem } from '../../utils'
import { ComponentSlotStylesInput, ICSSInJSStyle } from '../../../types'
import { LabelProps } from '../../../../components/Label/Label'

Expand Down
2 changes: 1 addition & 1 deletion src/themes/teams/components/Label/labelVariables.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { pxToRem } from '../../../../lib'
import { pxToRem } from '../../utils'

export default () => {
const color = 'rgba(0, 0, 0, 0.6)'
Expand Down
2 changes: 1 addition & 1 deletion src/themes/teams/components/List/listItemStyles.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { pxToRem } from '../../../../lib'
import { pxToRem } from '../../utils'
import { ComponentSlotStylesInput, ICSSInJSStyle } from '../../../types'
import { ListItemProps } from '../../../../components/List/ListItem'

Expand Down
2 changes: 1 addition & 1 deletion src/themes/teams/components/Menu/menuItemStyles.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { pxToRem } from '../../../../lib'
import { pxToRem } from '../../utils'
import { ComponentSlotStyleFunction, ComponentSlotStylesInput, ICSSInJSStyle } from '../../../types'
import { MenuVariables } from './menuVariables'
import { MenuItemProps, MenuItemState } from '../../../../components/Menu/MenuItem'
Expand Down
2 changes: 1 addition & 1 deletion src/themes/teams/components/Menu/menuStyles.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { pxToRem } from '../../../../lib'
import { pxToRem } from '../../utils'
import { ComponentSlotStylesInput, ICSSInJSStyle } from '../../../types'
import { MenuProps } from '../../../../components/Menu/Menu'

Expand Down
2 changes: 1 addition & 1 deletion src/themes/teams/components/Menu/menuVariables.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { pxToRem } from '../../../../lib'
import { pxToRem } from '../../utils'

export interface MenuVariables {
defaultColor: string
Expand Down
2 changes: 1 addition & 1 deletion src/themes/teams/components/Popup/popupContentStyles.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ComponentSlotStylesInput, ICSSInJSStyle } from '../../../types'
import { pxToRem } from '../../../../lib'
import { pxToRem } from '../../utils'
import { PopupContentProps } from '../../../../components/Popup/PopupContent'
import { PopupContentVariables } from './popupContentVariables'

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { pxToRem } from '../../../../lib'
import { pxToRem } from '../../utils'

export interface PopupContentVariables {
[key: string]: string | number
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {
RadioGroupItemProps,
RadioGroupItemState,
} from '../../../../components/RadioGroup/RadioGroupItem'
import { pxToRem } from '../../../../lib'
import { pxToRem } from '../../utils'

const radioStyles: ComponentSlotStylesInput<RadioGroupItemProps & RadioGroupItemState, any> = {
root: ({ props }): ICSSInJSStyle => ({
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { pxToRem } from '../../../../lib'
import { pxToRem } from '../../utils'
import { RadioGroupItemProps } from '../../../../components/RadioGroup/RadioGroupItem'

export default (siteVars: any, props: RadioGroupItemProps) => {
Expand Down
2 changes: 1 addition & 1 deletion src/themes/teams/components/Status/statusStyles.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { pxToRem } from '../../../../lib'
import { pxToRem } from '../../utils'
import { ComponentSlotStylesInput, ICSSInJSStyle } from '../../../types'
import { StatusPropsWithDefaults } from '../../../../components/Status/Status'
import { StatusVariables } from './statusVariables'
Expand Down
2 changes: 1 addition & 1 deletion src/themes/teams/components/Tree/treeItemStyles.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ICSSInJSStyle } from '../../../types'
import { pxToRem } from '../../../../lib'
import { pxToRem } from '../../utils'

const treeItemStyles = {
root: (): ICSSInJSStyle => ({
Expand Down
2 changes: 1 addition & 1 deletion src/themes/teams/components/Tree/treeStyles.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ICSSInJSStyle } from '../../../types'
import { pxToRem } from '../../../../lib'
import { pxToRem } from '../../utils'

const treeStyles = {
root: (): ICSSInJSStyle => ({
Expand Down
2 changes: 1 addition & 1 deletion src/themes/teams/components/Tree/treeTitleStyles.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ICSSInJSStyle } from '../../../types'
import { pxToRem } from '../../../../lib'
import { pxToRem } from '../../utils'

const treeTitleStyles = {
root: ({ variables }): ICSSInJSStyle => ({
Expand Down
3 changes: 1 addition & 2 deletions src/themes/teams/siteVariables.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { pxToRem } from '../../lib'
import { pxToRem } from './utils'
import { colors } from './colors'

//
Expand Down Expand Up @@ -94,7 +94,6 @@ export const bodyPadding = 0
export const bodyMargin = 0
export const bodyFontFamily =
'"Segoe UI", "Helvetica Neue", "Apple Color Emoji", "Segoe UI Emoji", Helvetica, Arial, sans-serif'
export const bodyFontSize = '1.4rem'
export const bodyBackground = white
export const bodyColor = black
export const bodyLineHeight = lineHeightBase
1 change: 0 additions & 1 deletion src/themes/teams/staticStyles/globalStyles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ const globalStyles: StaticStyleFunction = siteVars => ({
padding: siteVars.bodyPadding,
margin: siteVars.bodyMargin,
fontFamily: siteVars.bodyFontFamily,
fontSize: siteVars.bodyFontSize,
lineHeight: siteVars.bodyLineHeight,
},
})
Expand Down
5 changes: 5 additions & 0 deletions src/themes/teams/utils/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { pxToRem as basePxToRem } from '../../../lib'

const themeFontSizeInPx = 14

export const pxToRem = (sizeInPx: number) => basePxToRem(sizeInPx, themeFontSizeInPx)
Loading