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

✨Overrideable component spike #2410

Merged
merged 10 commits into from
Aug 16, 2022
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,6 @@ export default {
export const Introduction: Story<ButtonProps> = (args) => {
return <Button {...args}>You can control me</Button>
}
Introduction.args = {
as: undefined,
}
Introduction.decorators = [
(Story) => (
<Stack>
Expand Down
17 changes: 8 additions & 9 deletions packages/eds-core-react/src/components/Button/Button.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { forwardRef, ElementType, ButtonHTMLAttributes } from 'react'
import { forwardRef, ButtonHTMLAttributes } from 'react'
import styled, { css, ThemeProvider } from 'styled-components'
import { token as buttonToken } from './tokens'
import { ButtonTokenSet, ButtonToken } from './Button.types'
Expand All @@ -8,6 +8,7 @@ import {
outlineTemplate,
spacingsTemplate,
useToken,
OverridableComponent,
} from '@equinor/eds-utils'
import { InnerFullWidth } from './InnerFullWidth'
import { useEds } from '../EdsProvider'
Expand Down Expand Up @@ -70,6 +71,7 @@ const ButtonBase = styled.button(({ theme }: { theme: ButtonToken }) => {
const { focus, hover, disabled } = states

return css`
box-sizing: border-box;
margin: 0;
padding: 0;
text-decoration: none;
Expand Down Expand Up @@ -156,8 +158,6 @@ export type ButtonProps = {
href?: string
/** Is the button disabled */
disabled?: boolean
/** Change html element. */
as?: ElementType
/** Type of button
* @default 'button'
*/
Expand All @@ -166,8 +166,8 @@ export type ButtonProps = {
fullWidth?: boolean
} & ButtonHTMLAttributes<HTMLButtonElement>

export const Button = forwardRef<HTMLButtonElement, ButtonProps>(
function Button(
export const Button: OverridableComponent<ButtonProps, HTMLButtonElement> =
forwardRef(function Button(
{
color = 'primary',
variant = 'contained',
Expand All @@ -183,8 +183,8 @@ export const Button = forwardRef<HTMLButtonElement, ButtonProps>(
const { density } = useEds()
const token = useToken({ density }, getToken(variant, color))

const as: ElementType =
href && !disabled ? 'a' : other.as ? other.as : 'button'
const as = href && !disabled ? 'a' : other.as ? other.as : 'button'

const type = href || other.as ? undefined : 'button'

tabIndex = disabled ? -1 : tabIndex
Expand All @@ -210,5 +210,4 @@ export const Button = forwardRef<HTMLButtonElement, ButtonProps>(
</ButtonBase>
</ThemeProvider>
)
},
)
})
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ exports[`Button Matches snapshot 1`] = `
}

.c0 {
box-sizing: border-box;
margin: 0;
padding: 0;
-webkit-text-decoration: none;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ exports[`Pagination Matches snapshot 1`] = `
}

.c3 {
box-sizing: border-box;
margin: 0;
padding: 0;
-webkit-text-decoration: none;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ exports[`Search Matches snapshot 1`] = `
}

.c3 {
box-sizing: border-box;
margin: 0;
padding: 0;
-webkit-text-decoration: none;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ exports[`MultiSelect Matches snapshot 2`] = `
}

.c0 {
box-sizing: border-box;
margin: 0;
padding: 0;
-webkit-text-decoration: none;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ exports[`SingleSelect Matches snapshot 1`] = `
}

.c6 {
box-sizing: border-box;
margin: 0;
padding: 0;
-webkit-text-decoration: none;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ exports[`SideSheet Matches snapshot 1`] = `
}

.c3 {
box-sizing: border-box;
margin: 0;
padding: 0;
-webkit-text-decoration: none;
Expand Down
1 change: 1 addition & 0 deletions packages/eds-utils/src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@ export * from './templates'
export { joinHandlers } from './joinHandlers'
export { mergeRefs } from './mergeRefs'
export { setReactInputValue } from './setReactInputValue'
export type { OverridableComponent } from './overridableComponent'

export const trimSpaces = (text: string): string => text.replace(/ /g, '')
12 changes: 12 additions & 0 deletions packages/eds-utils/src/utils/overridableComponent.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { RefAttributes, FC, ElementType, ComponentPropsWithRef } from 'react'

export type OverridableComponent<Component, Element extends HTMLElement> = {
(props: Component & RefAttributes<Element>): ReturnType<FC>

<As extends ElementType>(
props: {
as: As
} & Component &
Omit<ComponentPropsWithRef<As>, keyof Component>,
): ReturnType<FC>
}