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

Add types to style mixins #26369

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
6 changes: 6 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@
"@types/requestidlecallback": "0.3.1",
"@types/semver": "7.2.0",
"@types/sprintf-js": "1.1.2",
"@types/tinycolor2": "1.4.2",
"@types/uuid": "7.0.2",
"@types/webpack": "4.41.16",
"@types/webpack-sources": "0.1.7",
Expand Down
20 changes: 15 additions & 5 deletions packages/components/src/utils/hooks/use-controlled-state.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,14 @@ import { useEffect, useState } from '@wordpress/element';
*/
import { isValueDefined, getDefinedValue } from '../values';

/**
* @template T
* @typedef Options
* @property {T} [initial] The initial state.
* @property {T} [fallback] The state to use when no state is defined.
*/

/** @type {Options<any>} */
const defaultOptions = {
initial: undefined,
/**
Expand All @@ -33,10 +41,9 @@ const defaultOptions = {
* Unlike the basic useState hook, useControlledState's state can
* be updated if a new incoming prop value is changed.
*
* @param {any} currentState The current value.
* @param {Object} options Additional options for the hook.
* @param {any} options.initial The initial state.
* @param {any} options.fallback The state to use when no state is defined.
* @template T
* @param {T | undefined} currentState The current value.
* @param {Options<T>} [options] Additional options for the hook.
*
* @return {[*, Function]} The controlled value and the value setter.
*/
Expand All @@ -60,7 +67,10 @@ function useControlledState( currentState, options = defaultOptions ) {
fallback
);

const setState = ( nextState ) => {
const setState = (
/** @type {any} */
Copy link
Contributor Author

Choose a reason for hiding this comment

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

The template T isn't in scope here 😕

nextState
) => {
if ( ! hasCurrentState ) {
setInternalState( nextState );
}
Expand Down
5 changes: 4 additions & 1 deletion packages/components/src/utils/hooks/use-jump-step.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,10 @@ function useJumpStep( {
const [ isShiftKey, setIsShiftKey ] = useState( false );

useEffect( () => {
const handleShiftKeyToggle = ( event ) => {
const handleShiftKeyToggle = (
/** @type {KeyboardEvent} */
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This get's passed to window.addEventListener so we do not use the synthetic version from react

event
) => {
setIsShiftKey( event.shiftKey );
};

Expand Down
5 changes: 4 additions & 1 deletion packages/components/src/utils/hooks/use-update-effect.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@
*/
import { useRef, useEffect } from '@wordpress/element';

/*
/**
* A `React.useEffect` that will not run on the first render.
* Source:
* https://github.com/reakit/reakit/blob/master/packages/reakit-utils/src/useUpdateEffect.ts
*
* @param {import('react').EffectCallback} effect The effect to run.
* @param {import('react').DependencyList} [deps] The dependencies of the effect.
*/
function useUpdateEffect( effect, deps ) {
const mounted = useRef( false );
Expand Down
23 changes: 9 additions & 14 deletions packages/components/src/utils/math.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,24 +19,23 @@ export function getNumber( value ) {
/**
* Safely adds 2 values.
*
* @param {number|string} args Values to add together.
* @param {(number|string)[]} args Values to add together.
*
* @return {number} The sum of values.
*/
export function add( ...args ) {
return args.reduce( ( sum, arg ) => sum + getNumber( arg ), 0 );
return args.map( getNumber ).reduce( ( sum, arg ) => sum + arg, 0 );
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This and the change below should be equivalent in runtime but appeases the typechecking (I couldn't figure out how to communicate to TS through JSDoc that reduce was going to be called as reduce<string|number, number>. It insisted that the return type of reduce be string|number. Mapping the array to number from string|number first avoids this. From a performance perspective I think this is negligible (you'd only start to notice if we were adding several thousand numbers, right?)

}

/**
* Safely subtracts 2 values.
*
* @param {number|string} args Values to subtract together.
* @param {(number|string)[]} args Values to subtract together.
*
* @return {number} The difference of the 2 values.
*/
export function subtract( ...args ) {
return args.reduce( ( diff, arg, index ) => {
const value = getNumber( arg );
return args.map( getNumber ).reduce( ( diff, value, index ) => {
return index === 0 ? value : diff - value;
} );
}
Expand All @@ -56,10 +55,10 @@ function getPrecision( value ) {
/**
* Clamps a value based on a min/max range with rounding
*
* @param {number} value The value.
* @param {number} min The minimum range.
* @param {number} max The maximum range.
* @param {number} step A multiplier for the value.
* @param {number} [value=0] The value.
* @param {number} [min=Infinity] The minimum range.
* @param {number} [max=Infinity] The maximum range.
* @param {number} [step=1] A multiplier for the value.
*
* @return {number} The rounded and clamped value.
*/
Expand All @@ -84,11 +83,7 @@ export function roundClamp(
* Clamps a value based on a min/max range with rounding.
* Returns a string.
*
* @param {any} args Arguments for roundClamp().
* @property {number} value The value.
* @property {number} min The minimum range.
* @property {number} max The maximum range.
* @property {number} step A multiplier for the value.
* @param {Parameters<roundClamp>} args Arguments for roundClamp().
*
* @return {string} The rounded and clamped value.
*/
Expand Down
10 changes: 5 additions & 5 deletions packages/components/src/utils/rtl.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,9 @@ function getConvertedKey( key ) {
/**
* An incredibly basic ltr -> rtl converter for style properties
*
* @param {Object} ltrStyles
* @param {Record<string, string>} ltrStyles
*
* @return {Object} Converted ltr -> rtl styles
* @return {Record<string, string>} Converted ltr -> rtl styles
*/
export const convertLTRToRTL = ( ltrStyles = {} ) => {
return mapKeys( ltrStyles, ( _value, key ) => getConvertedKey( key ) );
Expand All @@ -76,10 +76,10 @@ export const convertLTRToRTL = ( ltrStyles = {} ) => {
/**
* A higher-order function that create an incredibly basic ltr -> rtl style converter for CSS objects.
*
* @param {Object} ltrStyles Ltr styles. Converts and renders from ltr -> rtl styles, if applicable.
* @param {null|Object} rtlStyles Rtl styles. Renders if provided.
* @param {Record<string, string>} [ltrStyles={}] Ltr styles. Converts and renders from ltr -> rtl styles, if applicable.
* @param {Record<string, string>} [rtlStyles] Rtl styles. Renders if provided.
*
* @return {Function} A function to output CSS styles for Emotion's renderer
* @return {() => ReturnType<css>} A function to output CSS styles for Emotion's renderer
*/
export function rtl( ltrStyles = {}, rtlStyles ) {
return () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/components/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@
"declarationDir": "build-types"
},
"references": [ { "path": "../primitives" } ],
"include": [ "src/dashicon/*", "src/tip/*" ]
"include": [ "src/dashicon/*", "src/tip/*", "src/utils/**/*" ]
}