Skip to content
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

@theme-ui/match-media : TypeScript conversion #696

Merged
merged 4 commits into from
Feb 20, 2020
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
2 changes: 1 addition & 1 deletion packages/css/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -511,7 +511,7 @@ export type ColorMode = {
}

export interface Theme {
breakpoints?: ObjectOrArray<number | string | symbol>
breakpoints?: Array<string>
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

According to the documentation this is only ever a string[]. Also was necessary to fix type issues in match-media

Copy link
Member

Choose a reason for hiding this comment

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

I'd guess the types for this might have originated in older versions of Styled System FWIW -- might be some other holdovers from that theme interface to keep an eye out for

mediaQueries?: { [size: string]: string }
space?: ObjectOrArray<CSS.MarginProperty<number | string>>
fontSizes?: ObjectOrArray<CSS.FontSizeProperty<number>>
Expand Down
9 changes: 6 additions & 3 deletions packages/match-media/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,24 @@
"name": "@theme-ui/match-media",
"version": "0.3.1",
"description": "React hooks for theme-ui breakpoints",
"source": "src/index.ts",
"main": "dist/index.js",
"module": "dist/index.esm.js",
"types": "dist/index.d.ts",
"author": "Brent Jackson <[email protected]>",
"license": "MIT",
"repository": "system-ui/theme-ui",
"scripts": {
"prepare": "microbundle --no-compress",
"watch": "microbundle watch --no-compress"
"prepare": "microbundle --no-compress --tsconfig tsconfig.json",
"watch": "microbundle watch --no-compress --tsconfig tsconfig.json"
},
"publishConfig": {
"access": "public"
},
"devDependencies": {
"react": "^16.9.0",
"theme-ui": "^0.3.1"
"@theme-ui/core": "^0.3.1",
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Switch out theme-ui for /core (to access Theme)

"@theme-ui/css": "^0.3.1"
},
"peerDependencies": {
"react": "^16.9.0",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
import { useState, useEffect, useCallback } from 'react'
import { useThemeUI } from 'theme-ui'
import { useThemeUI } from '@theme-ui/core'
import { Theme } from '@theme-ui/css'

// Shared with @theme-ui/css
const defaultBreakpoints = [40, 52, 64].map(n => n + 'em')

export const useBreakpointIndex = (options = {}) => {
type defaultOptions = {
defaultIndex?: number
}

export const useBreakpointIndex = (options: defaultOptions = {}) => {
const context = useThemeUI()
const { defaultIndex = 0 } = options
const breakpoints =
Expand Down Expand Up @@ -47,7 +52,9 @@ export const useBreakpointIndex = (options = {}) => {
return value
}

export const useResponsiveValue = (values, options) => {
type Values = ((theme: Theme | null) => string[]) | string[]

export const useResponsiveValue = (values: Values, options: defaultOptions = {}) => {
const { theme } = useThemeUI()
const array = typeof values === 'function' ? values(theme) : values
const index = useBreakpointIndex(options)
Expand Down
8 changes: 8 additions & 0 deletions packages/match-media/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"compilerOptions": {
"resolveJsonModule": true,
"esModuleInterop": true,
"moduleResolution": "node",
"strict": true
}
}