-
Notifications
You must be signed in to change notification settings - Fork 1
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
UNSAFE classname test #1826
Draft
pavelklibani
wants to merge
5
commits into
main
Choose a base branch
from
refactor/ds-1176-unsafe-classname
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
UNSAFE classname test #1826
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
d8cc0a0
UNSAFE classname test
pavelklibani ca89643
fixup! UNSAFE classname test
pavelklibani d2025c1
Refactor(web-react): Apply condition to styleProps
pavelklibani 7c69f84
Refactor(web-react): step further
pavelklibani 25ae27e
fixup! Refactor(web-react): step further
pavelklibani File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,13 @@ | ||
@import '@lmc-eu/spirit-web/src/scss'; | ||
|
||
.test { | ||
font-weight: bold; | ||
|
||
&::before { | ||
content: '✅ '; | ||
} | ||
} | ||
|
||
p:not(.test)::before { | ||
content: '❌ '; | ||
} |
9 changes: 9 additions & 0 deletions
9
examples/next-with-app-router/src/app/test1/components/OutsideComponent.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
'use client'; | ||
|
||
import React from 'react'; | ||
|
||
const OutsideComponent = (props: any) => { | ||
return <p {...props}>{props.children}</p>; | ||
}; | ||
|
||
export default OutsideComponent; |
31 changes: 31 additions & 0 deletions
31
examples/next-with-app-router/src/app/test1/components/TestComponent.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
'use client'; | ||
|
||
import React, { ElementType, ReactNode } from 'react'; | ||
import { StyleProps, TransferProps } from '../../../../../../packages/web-react/src/types'; | ||
// import { useStyleProps } from './originalStyleProps'; | ||
import { useStyleProps } from './newStyleProps'; | ||
import classNames from 'classnames'; | ||
|
||
interface TestComponentProps extends StyleProps, TransferProps { | ||
elementType?: ElementType | string; | ||
children?: string | ReactNode; | ||
} | ||
|
||
const defaultProps: TestComponentProps = { | ||
elementType: 'p', | ||
}; | ||
|
||
const TestComponent = (props: TestComponentProps) => { | ||
const propsWithDefaults = { ...defaultProps, ...props }; | ||
const { elementType: ElementTag = 'p', children, ...rest } = propsWithDefaults; | ||
|
||
const { styleProps, props: transferProps } = useStyleProps({ ElementTag, ...rest }); | ||
|
||
return ( | ||
<ElementTag {...transferProps} {...styleProps}> | ||
{children} | ||
</ElementTag> | ||
); | ||
}; | ||
|
||
export default TestComponent; |
67 changes: 67 additions & 0 deletions
67
examples/next-with-app-router/src/app/test1/components/newStyleProps.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import classNames from 'classnames'; | ||
import { CSSProperties, HTMLAttributes, useContext } from 'react'; | ||
import { warning } from '../../../../../../packages/web-react/src//common/utilities'; | ||
import ClassNamePrefixContext from '../../../../../../packages/web-react/src/context/ClassNamePrefixContext'; | ||
import { StyleProps } from '../../../../../../packages/web-react/src//types'; | ||
import { useStyleUtilities } from '../../../../../../packages/web-react/src/hooks/useStyleUtilities'; | ||
|
||
export type StylePropsResult = { | ||
styleProps: HTMLAttributes<HTMLElement>; | ||
props: HTMLAttributes<HTMLElement>; | ||
}; | ||
|
||
export function useStyleProps<T extends StyleProps & { ElementTag?: any }>(props: T): StylePropsResult { | ||
const classNamePrefix = useContext(ClassNamePrefixContext); | ||
const { UNSAFE_className, UNSAFE_style, ElementTag, ...otherProps } = props; | ||
const { styleUtilities, props: modifiedProps } = useStyleUtilities(otherProps, classNamePrefix); | ||
|
||
const style: CSSProperties = { ...UNSAFE_style }; | ||
|
||
if (typeof ElementTag !== 'function') { | ||
// Want to check if className prop exists, but not to define it in StyleProps type | ||
// @ts-expect-error Property 'className' does not exist on type 'Omit<T, "UNSAFE_className" | "UNSAFE_style">'. | ||
if (modifiedProps.className) { | ||
warning( | ||
false, | ||
'The className prop is unsafe and is unsupported in Spirit Web React. ' + | ||
'Please use style props with Spirit Design Tokens, or UNSAFE_className if you absolutely must do something custom. ' + | ||
'Note that this may break in future versions due to DOM structure changes.', | ||
); | ||
|
||
// @ts-expect-error same as above, let me live my life | ||
delete modifiedProps.className; | ||
} | ||
|
||
// Want to check if style prop exists, but not to define it in StyleProps type | ||
// @ts-expect-error Property 'style' does not exist on type 'Omit<T, "UNSAFE_className" | "UNSAFE_style">'. | ||
if (modifiedProps.style) { | ||
warning( | ||
false, | ||
'The style prop is unsafe and is unsupported in Spirit Web React. ' + | ||
'Please use style props with Spirit Design Tokens, or UNSAFE_style if you absolutely must do something custom. ' + | ||
'Note that this may break in future versions due to DOM structure changes.', | ||
); | ||
|
||
// @ts-expect-error same as above, let me live my life | ||
delete modifiedProps.style; | ||
} | ||
|
||
const styleProps = { | ||
style: Object.keys(style).length > 0 ? style : undefined, | ||
className: classNames(UNSAFE_className, ...styleUtilities) || undefined, | ||
}; | ||
|
||
return { | ||
styleProps, | ||
props: modifiedProps as HTMLAttributes<HTMLElement>, | ||
}; | ||
} | ||
|
||
return { | ||
styleProps: { | ||
...(UNSAFE_style !== undefined && { UNSAFE_style }), | ||
...(UNSAFE_className !== undefined && { UNSAFE_className }), | ||
}, | ||
props: { ...modifiedProps }, | ||
}; | ||
} |
57 changes: 57 additions & 0 deletions
57
examples/next-with-app-router/src/app/test1/components/originalStyleProps.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import classNames from 'classnames'; | ||
import { CSSProperties, HTMLAttributes, useContext } from 'react'; | ||
import { warning } from '../../../../../../packages/web-react/src//common/utilities'; | ||
import ClassNamePrefixContext from '../../../../../../packages/web-react/src/context/ClassNamePrefixContext'; | ||
import { StyleProps } from '../../../../../../packages/web-react/src//types'; | ||
import { useStyleUtilities } from '../../../../../../packages/web-react/src/hooks/useStyleUtilities'; | ||
|
||
export type StylePropsResult = { | ||
styleProps: HTMLAttributes<HTMLElement>; | ||
props: HTMLAttributes<HTMLElement>; | ||
}; | ||
|
||
export function useStyleProps<T extends StyleProps>(props: T): StylePropsResult { | ||
const classNamePrefix = useContext(ClassNamePrefixContext); | ||
const { UNSAFE_className, UNSAFE_style, ...otherProps } = props; | ||
const { styleUtilities, props: modifiedProps } = useStyleUtilities(otherProps, classNamePrefix); | ||
|
||
const style: CSSProperties = { ...UNSAFE_style }; | ||
|
||
// Want to check if className prop exists, but not to define it in StyleProps type | ||
// @ts-expect-error Property 'className' does not exist on type 'Omit<T, "UNSAFE_className" | "UNSAFE_style">'. | ||
if (modifiedProps.className) { | ||
warning( | ||
false, | ||
'The className prop is unsafe and is unsupported in Spirit Web React. ' + | ||
'Please use style props with Spirit Design Tokens, or UNSAFE_className if you absolutely must do something custom. ' + | ||
'Note that this may break in future versions due to DOM structure changes.', | ||
); | ||
|
||
// @ts-expect-error same as above, let me live my life | ||
delete modifiedProps.className; | ||
} | ||
|
||
// Want to check if style prop exists, but not to define it in StyleProps type | ||
// @ts-expect-error Property 'style' does not exist on type 'Omit<T, "UNSAFE_className" | "UNSAFE_style">'. | ||
if (modifiedProps.style) { | ||
warning( | ||
false, | ||
'The style prop is unsafe and is unsupported in Spirit Web React. ' + | ||
'Please use style props with Spirit Design Tokens, or UNSAFE_style if you absolutely must do something custom. ' + | ||
'Note that this may break in future versions due to DOM structure changes.', | ||
); | ||
|
||
// @ts-expect-error same as above, let me live my life | ||
delete modifiedProps.style; | ||
} | ||
|
||
const styleProps = { | ||
style: Object.keys(style).length > 0 ? style : undefined, | ||
className: classNames(UNSAFE_className, ...styleUtilities) || undefined, | ||
}; | ||
|
||
return { | ||
styleProps, | ||
props: modifiedProps as HTMLAttributes<HTMLElement>, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import React from 'react'; | ||
import { Container, Heading, Text as SpiritText } from '@lmc-eu/spirit-web-react'; | ||
import TestComponent from './components/TestComponent'; | ||
import OutsideComponent from './components/OutsideComponent'; | ||
|
||
const page = () => { | ||
return ( | ||
<Container marginTop="space-1000" marginBottom="space-1000"> | ||
<Heading elementType="h1" size="medium" marginBottom="space-1000"> | ||
Test component 1 | ||
</Heading> | ||
|
||
<Heading elementType="h2" size="small" marginBottom="space-1000"> | ||
Should work | ||
</Heading> | ||
|
||
{/* TESTING ⬇ */} | ||
<> | ||
<TestComponent UNSAFE_className="test" UNSAFE_style={{ color: 'green' }}> | ||
Spirit Text component without elementType | ||
</TestComponent> | ||
|
||
<TestComponent elementType="p" UNSAFE_className="test" UNSAFE_style={{ color: 'green' }}> | ||
HTML element "p" with UNSAFE class and style | ||
</TestComponent> | ||
|
||
<TestComponent elementType={SpiritText} UNSAFE_className="test" UNSAFE_style={{ color: 'green' }}> | ||
Spirit Text component with UNSAFE class and style | ||
</TestComponent> | ||
|
||
<TestComponent elementType={OutsideComponent} className="test" style={{ color: 'green' }}> | ||
Random component with class and style | ||
</TestComponent> | ||
</> | ||
{/* TESTING ⬆ */} | ||
|
||
<Heading elementType="h2" size="small" marginBottom="space-1000" marginTop="space-1000"> | ||
Should not work | ||
</Heading> | ||
|
||
{/* TESTING ⬇ */} | ||
<> | ||
{/* This will not be working - Spirit component accepts only UNSAFE style props */} | ||
<TestComponent elementType={SpiritText} className="test" style={{ color: 'green' }}> | ||
Spirit Text component with class and style | ||
</TestComponent> | ||
|
||
{/* This will not be working - Random component doesn't know what UNSAFE style props are */} | ||
<TestComponent elementType={OutsideComponent} UNSAFE_className="test" UNSAFE_style={{ color: 'green' }}> | ||
Random component with UNSAFE class and style | ||
</TestComponent> | ||
</> | ||
{/* TESTING ⬆ */} | ||
</Container> | ||
); | ||
}; | ||
|
||
export default page; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
suggestion: I think, we will use something like
Button.spiritComponent = Button
for cases where we want to check if it is a Spirit component or if we want a name.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Or we can use
Button.isSpiritComponent
first and reimplement it when needed.