Skip to content

Commit

Permalink
Cherrypicking react-button changes from 7.0 into master (#15961)
Browse files Browse the repository at this point in the history
* Merging with latest.

* updating change file versions.
  • Loading branch information
dzearing authored Nov 17, 2020
1 parent e0e80df commit 16bb581
Show file tree
Hide file tree
Showing 35 changed files with 174 additions and 129 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"type": "prerelease",
"comment": "Updating useStatus and useAvatar to return state only, exporting render functions separately.",
"packageName": "@fluentui/react-avatar",
"email": "[email protected]",
"dependentChangeType": "patch",
"date": "2020-10-13T19:47:23.741Z"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"type": "prerelease",
"comment": "Updating button hooks to return state only; render function should be imported separately.",
"packageName": "@fluentui/react-button",
"email": "[email protected]",
"dependentChangeType": "patch",
"date": "2020-10-13T01:23:22.018Z"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"type": "prerelease",
"comment": "Updating the README example in compose/next.",
"packageName": "@fluentui/react-compose",
"email": "[email protected]",
"dependentChangeType": "patch",
"date": "2020-10-13T01:23:32.010Z"
}
5 changes: 3 additions & 2 deletions packages/react-avatar/src/components/Avatar/Avatar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@ import { useInlineTokens } from '@fluentui/react-theme-provider';
import { useFocusRects, nullRender } from '@fluentui/utilities';
import * as classes from './Avatar.scss';
import { Badge } from '../Badge/Badge';
import { renderAvatar } from './renderAvatar';

const useAvatarClasses = makeClasses(classes);

export const Avatar = React.forwardRef((props: AvatarProps, ref: React.Ref<HTMLElement>) => {
const { state, render } = useAvatar(props, ref, {
const state = useAvatar(props, ref, {
badge: { as: props.badge ? Badge : nullRender },
});

Expand All @@ -19,7 +20,7 @@ export const Avatar = React.forwardRef((props: AvatarProps, ref: React.Ref<HTMLE
useFocusRects(state.ref);
useInlineTokens(state, '--avatar');

return render(state);
return renderAvatar(state);
});

Avatar.displayName = 'Avatar';
3 changes: 2 additions & 1 deletion packages/react-avatar/src/components/Avatar/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from './Avatar.types';
export * from './useAvatar';
export * from './Avatar';
export * from './renderAvatar';
export * from './useAvatar';
15 changes: 15 additions & 0 deletions packages/react-avatar/src/components/Avatar/renderAvatar.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import * as React from 'react';
import { getSlots } from '@fluentui/react-compose/lib/next/index';
import { AvatarState } from './Avatar.types';
import { avatarShorthandProps } from './useAvatar';

export const renderAvatar = (state: AvatarState) => {
const { slots, slotProps } = getSlots(state, avatarShorthandProps);
return (
<slots.root {...slotProps.root}>
<slots.label {...slotProps.label} />
<slots.image {...slotProps.image} />
<slots.badge {...slotProps.badge} />
</slots.root>
);
};
20 changes: 4 additions & 16 deletions packages/react-avatar/src/components/Avatar/useAvatar.tsx
Original file line number Diff line number Diff line change
@@ -1,25 +1,13 @@
import * as React from 'react';
import { makeMergeProps, getSlots, resolveShorthandProps } from '@fluentui/react-compose/lib/next/index';
import { AvatarProps, AvatarState, defaultAvatarSize } from './Avatar.types';
import { makeMergeProps, resolveShorthandProps } from '@fluentui/react-compose/lib/next/index';
import { AvatarProps, defaultAvatarSize } from './Avatar.types';
import { calcAvatarStyleProps } from './calcAvatarStyleProps';
import { useMergedRefs } from '@fluentui/react-hooks';
import { getInitials as defaultGetInitials, nullRender } from '@fluentui/utilities';
import { Image } from '../Image/index';
import { ContactIcon as DefaultAvatarIcon } from '@fluentui/react-icons-mdl2';

const avatarShorthandProps: (keyof AvatarProps)[] = ['label', 'image', 'badge'];

export const renderAvatar = (state: AvatarState) => {
const { slots, slotProps } = getSlots(state, avatarShorthandProps);

return (
<slots.root {...slotProps.root}>
<slots.label {...slotProps.label} />
<slots.image {...slotProps.image} />
<slots.badge {...slotProps.badge} />
</slots.root>
);
};
export const avatarShorthandProps: (keyof AvatarProps)[] = ['label', 'image', 'badge'];

const mergeProps = makeMergeProps({ deepMerge: avatarShorthandProps });

Expand Down Expand Up @@ -63,5 +51,5 @@ export const useAvatar = (props: AvatarProps, ref: React.Ref<HTMLElement>, defau
state.image = { as: nullRender };
}

return { state, render: renderAvatar };
return state;
};
5 changes: 3 additions & 2 deletions packages/react-avatar/src/components/Badge/Badge.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,18 @@ import { useInlineTokens } from '@fluentui/react-theme-provider';
import { useBadge } from './useBadge';
import { BadgeProps } from './Badge.types';
import * as classes from './Badge.scss';
import { renderBadge } from './renderBadge';

// Create a hook to resolve classnames.
export const useBadgeClasses = makeClasses(classes);

export const Badge = React.forwardRef((props: BadgeProps, ref: React.Ref<HTMLElement>) => {
const { render, state } = useBadge(props, ref);
const state = useBadge(props, ref);

useBadgeClasses(state);
useInlineTokens(state, '--badge');

return render(state);
return renderBadge(state);
});

Badge.displayName = 'Badge';
4 changes: 3 additions & 1 deletion packages/react-avatar/src/components/Badge/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
export * from './Badge.types';
export * from './useBadge';
export * from './Badge';
export * from './renderBadge';
export * from './useBadge';
export * from './useBadge';
13 changes: 13 additions & 0 deletions packages/react-avatar/src/components/Badge/renderBadge.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import * as React from 'react';
import { BadgeState } from './Badge.types';
import { getSlots } from '@fluentui/react-compose/lib/next/index';
import { badgeShorthandProps } from './useBadge';

export const renderBadge = (state: BadgeState) => {
const { slots, slotProps } = getSlots(state, badgeShorthandProps);
return (
<slots.root {...slotProps.root}>
<slots.icon {...slotProps.icon} />
</slots.root>
);
};
16 changes: 3 additions & 13 deletions packages/react-avatar/src/components/Badge/useBadge.tsx
Original file line number Diff line number Diff line change
@@ -1,20 +1,10 @@
import * as React from 'react';
import { BadgeProps, BadgeState } from './Badge.types';
import { getSlots, resolveShorthandProps, makeMergeProps } from '@fluentui/react-compose/lib/next/index';
import { BadgeProps } from './Badge.types';
import { resolveShorthandProps, makeMergeProps } from '@fluentui/react-compose/lib/next/index';
import { useMergedRefs } from '@fluentui/react-hooks';

export const badgeShorthandProps: (keyof BadgeProps)[] = ['icon'];

export const renderBadge = (state: BadgeState) => {
const { slots, slotProps } = getSlots(state, badgeShorthandProps);

return (
<slots.root {...slotProps.root}>
<slots.icon {...slotProps.icon} />
</slots.root>
);
};

const mergeProps = makeMergeProps({ deepMerge: badgeShorthandProps });

export const useBadge = (props: BadgeProps, ref: React.Ref<HTMLElement>, defaultProps?: BadgeProps) => {
Expand All @@ -32,5 +22,5 @@ export const useBadge = (props: BadgeProps, ref: React.Ref<HTMLElement>, default
},
);

return { state, render: renderBadge };
return state;
};
3 changes: 2 additions & 1 deletion packages/react-avatar/src/components/Image/index.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from './Image.types';
export * from './useImage';
export * from './Image';
export * from './renderImage';
export * from './useImage';
8 changes: 8 additions & 0 deletions packages/react-avatar/src/components/Image/renderImage.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import * as React from 'react';
import { ImageState } from './Image.types';
import { getSlots } from '@fluentui/react-compose/lib/next/index';

export const renderImage = (state: ImageState) => {
const { slots, slotProps } = getSlots(state);
return <slots.root {...slotProps.root} />;
};
9 changes: 2 additions & 7 deletions packages/react-avatar/src/components/Image/useImage.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
import * as React from 'react';
import { ImageProps, ImageState } from './Image.types';
import { getSlots, makeMergeProps } from '@fluentui/react-compose/lib/next/index';

export const renderImage = (state: ImageState) => {
const { slots, slotProps } = getSlots(state);

return <slots.root {...slotProps.root} />;
};
import { makeMergeProps } from '@fluentui/react-compose/lib/next/index';
import { renderImage } from './renderImage';

const mergeProps = makeMergeProps<ImageState>();

Expand Down
41 changes: 21 additions & 20 deletions packages/react-button/etc/react-button.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,22 @@ export type MenuButtonTokens = ButtonTokens & {
// @public (undocumented)
export type MenuButtonVariants = ButtonVariants<MenuButtonTokens>;

// @public
const renderButton: (state: ButtonState) => JSX.Element;

export { renderButton }

export { renderButton as renderToggleButton }

// @public
export const renderCompoundButton: (state: CompoundButtonState) => JSX.Element;

// @public
export const renderMenuButton: (state: MenuButtonState) => JSX.Element;

// @public
export const renderSplitButton: (state: SplitButtonState) => JSX.Element;

// @public
export const SplitButton: React.ForwardRefExoticComponent<Pick<SplitButtonProps, string | number> & React.RefAttributes<HTMLElement>>;

Expand Down Expand Up @@ -260,10 +276,7 @@ export type ToggleButtonTokens = ButtonTokens;
export type ToggleButtonVariants = ButtonVariants<ToggleButtonTokens>;

// @public
export const useButton: (props: ButtonProps, ref: React.Ref<HTMLElement>, defaultProps?: ButtonProps | undefined) => {
state: Record<string, any>;
render: (state: import("./Button.types").ButtonState) => JSX.Element;
};
export const useButton: (props: ButtonProps, ref: React.Ref<HTMLElement>, defaultProps?: ButtonProps | undefined) => ButtonState;

// @public (undocumented)
export const useButtonClasses: (state: ButtonState, options?: import("@fluentui/react-theme-provider").UseStylesOptions | undefined) => void;
Expand All @@ -275,10 +288,7 @@ export const useButtonState: (draftState: ButtonState) => void;
export const useChecked: <TDraftState extends CheckedState>(draftState: TDraftState) => void;

// @public
export const useCompoundButton: (props: CompoundButtonProps, ref: React.Ref<HTMLElement>, defaultProps?: CompoundButtonProps | undefined) => {
state: Record<string, any>;
render: (state: import("./CompoundButton.types").CompoundButtonState) => JSX.Element;
};
export const useCompoundButton: (props: CompoundButtonProps, ref: React.Ref<HTMLElement>, defaultProps?: CompoundButtonProps | undefined) => CompoundButtonState;

// @public (undocumented)
export const useCompoundButtonClasses: (state: CompoundButtonState) => void;
Expand All @@ -287,10 +297,7 @@ export const useCompoundButtonClasses: (state: CompoundButtonState) => void;
export const useExpanded: <TDraftState extends ExpandedState>(draftState: TDraftState) => void;

// @public
export const useMenuButton: (props: MenuButtonProps, ref: React.Ref<HTMLElement>, defaultProps?: MenuButtonProps | undefined) => {
state: MenuButtonState;
render: (state: MenuButtonState) => JSX.Element;
};
export const useMenuButton: (props: MenuButtonProps, ref: React.Ref<HTMLElement>, defaultProps?: MenuButtonProps | undefined) => MenuButtonState;

// @public (undocumented)
export const useMenuButtonClasses: (state: MenuButtonState) => void;
Expand All @@ -299,19 +306,13 @@ export const useMenuButtonClasses: (state: MenuButtonState) => void;
export const useMenuButtonState: (state: MenuButtonState) => void;

// @public
export const useSplitButton: (props: SplitButtonProps, ref: React.Ref<HTMLElement>, defaultProps?: SplitButtonProps | undefined) => {
state: SplitButtonState;
render: (state: SplitButtonState) => JSX.Element;
};
export const useSplitButton: (props: SplitButtonProps, ref: React.Ref<HTMLElement>, defaultProps?: SplitButtonProps | undefined) => SplitButtonState;

// @public (undocumented)
export const useSplitButtonClasses: (state: SplitButtonState, options?: import("@fluentui/react-theme-provider").UseStylesOptions | undefined) => void;

// @public (undocumented)
export const useToggleButton: (props: ToggleButtonProps, ref: React.Ref<HTMLElement>, defaultProps?: ToggleButtonProps | undefined) => {
state: Record<string, any>;
render: (state: import("../Button").ButtonState) => JSX.Element;
};
export const useToggleButton: (props: ToggleButtonProps, ref: React.Ref<HTMLElement>, defaultProps?: ToggleButtonProps | undefined) => ToggleButtonState;

// @public (undocumented)
export const useToggleButtonClasses: (state: ToggleButtonState) => void;
Expand Down
5 changes: 3 additions & 2 deletions packages/react-button/src/components/Button/Button.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,19 @@ import { useButton } from './useButton';
import { ButtonProps } from './Button.types';
import { useInlineTokens } from '@fluentui/react-theme-provider';
import { useButtonClasses } from './useButtonClasses';
import { renderButton } from './renderButton';

/**
* Define a styled Button, using the `useButton` hook.
* {@docCategory Button}
*/
export const Button = React.forwardRef<HTMLElement, ButtonProps>((props, ref) => {
const { render, state } = useButton(props, ref);
const state = useButton(props, ref);

useButtonClasses(state);
useInlineTokens(state, '--button');

return render(state);
return renderButton(state);
});

Button.displayName = 'Button';
5 changes: 3 additions & 2 deletions packages/react-button/src/components/Button/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export * from './Button.types';
export * from './useButtonClasses';
export * from './Button';
export * from './renderButton';
export * from './useButton';
export * from './useButtonClasses';
export * from './useButtonState';
export * from './Button';
5 changes: 2 additions & 3 deletions packages/react-button/src/components/Button/useButton.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import * as React from 'react';
import { makeMergeProps, resolveShorthandProps } from '@fluentui/react-compose/lib/next/index';
import { ButtonProps } from './Button.types';
import { ButtonProps, ButtonState } from './Button.types';
import { useButtonState } from './useButtonState';
import { renderButton } from './renderButton';

/**
* Consts listing which props are shorthand props.
Expand Down Expand Up @@ -33,5 +32,5 @@ export const useButton = (props: ButtonProps, ref: React.Ref<HTMLElement>, defau

useButtonState(state);

return { state, render: renderButton };
return state as ButtonState;
};
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,19 @@ import { useInlineTokens } from '@fluentui/react-theme-provider';
import { CompoundButtonProps } from './CompoundButton.types';
import { useCompoundButton } from './useCompoundButton';
import { useCompoundButtonClasses } from './useCompoundButtonClasses';
import { renderCompoundButton } from './renderCompoundButton';

/**
* Define a styled CompoundButton, using the `useCompoundButton` hook.
* {@docCategory Button}
*/
export const CompoundButton = React.forwardRef<HTMLElement, CompoundButtonProps>((props, ref) => {
const { render, state } = useCompoundButton(props, ref);
const state = useCompoundButton(props, ref);

useCompoundButtonClasses(state);
useInlineTokens(state, '--button');

return render(state);
return renderCompoundButton(state);
});

CompoundButton.displayName = 'CompoundButton';
3 changes: 2 additions & 1 deletion packages/react-button/src/components/CompoundButton/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export * from './CompoundButton';
export * from './CompoundButton.types';
export * from './CompoundButton';
export * from './renderCompoundButton';
export * from './useCompoundButton';
export * from './useCompoundButtonClasses';
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import * as React from 'react';
import { makeMergeProps, resolveShorthandProps } from '@fluentui/react-compose/lib/next/index';
import { CompoundButtonProps } from './CompoundButton.types';
import { CompoundButtonProps, CompoundButtonState } from './CompoundButton.types';
import { useButtonState } from '../Button/useButtonState';
import { renderCompoundButton } from './renderCompoundButton';

/**
* Consts listing which props are shorthand props.
Expand Down Expand Up @@ -41,5 +40,5 @@ export const useCompoundButton = (

useButtonState(state);

return { state, render: renderCompoundButton };
return state as CompoundButtonState;
};
Loading

0 comments on commit 16bb581

Please sign in to comment.