-
Notifications
You must be signed in to change notification settings - Fork 116
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(code-block): create Code Block Package
Co-authored-by: Shadi <TheSisb@users.noreply.github.com> Co-authored-by: gloriliale <77117846+gloriliale@users.noreply.github.com> Co-authored-by: Lee White <leewhite128@gmail.com>
- Loading branch information
1 parent
737423f
commit 29ebf59
Showing
44 changed files
with
2,058 additions
and
12 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
--- | ||
'@twilio-paste/code-block': major | ||
'@twilio-paste/core': minor | ||
--- | ||
|
||
[Code Block] create a new CodeBlock component, which allows you to display readable blocks of code 🎉 |
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,6 @@ | ||
--- | ||
'@twilio-paste/core': minor | ||
'@twilio-paste/syntax-highlighter-library': major | ||
--- | ||
|
||
[Syntax Highlighter] Create a new library, @twilio-paste/syntax-highlighter-library |
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,6 @@ | ||
--- | ||
'@twilio-paste/button': patch | ||
'@twilio-paste/core': patch | ||
--- | ||
|
||
[Button] support <Button as='a' variant='inverse'> |
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 |
---|---|---|
@@ -0,0 +1,9 @@ | ||
declare module 'react-syntax-highlighter/dist/esm/languages/prism/shell-session' { | ||
const language: any; | ||
export default language; | ||
} | ||
|
||
declare module 'react-syntax-highlighter/dist/esm/styles/prism/night-owl' { | ||
const style: { [key: string]: React.CSSProperties }; | ||
export default style; | ||
} |
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
Empty file.
211 changes: 211 additions & 0 deletions
211
packages/paste-core/components/code-block/__tests__/customization.spec.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,211 @@ | ||
import * as React from 'react'; | ||
import {CustomizationProvider} from '@twilio-paste/customization'; | ||
import {render, screen} from '@testing-library/react'; | ||
|
||
import {CodeBlock, CodeBlockWrapper, CodeBlockHeader, CodeBlockTabList, CodeBlockTab, CodeBlockTabPanel} from '../src'; | ||
|
||
const jsCode = `(num) => num + 1`; | ||
|
||
const CustomizationWrapper: React.FC = ({children}) => ( | ||
<CustomizationProvider | ||
baseTheme="default" | ||
theme={TestTheme} | ||
elements={{ | ||
CODE_BLOCK_CONTENT: {width: 'size50'}, | ||
CODE_BLOCK_COPY_BUTTON: {backgroundColor: 'colorBackgroundErrorWeakest'}, | ||
CODE_BLOCK_EXTERNAL_LINK: {backgroundColor: 'colorBackgroundErrorWeakest'}, | ||
CODE_BLOCK_HEADER: {borderTopRightRadius: 'borderRadius30'}, | ||
CODE_BLOCK_TAB_LIST: {columnGap: 'space0'}, | ||
CODE_BLOCK_TAB_PANEL: {borderBottomRightRadius: 'borderRadius30'}, | ||
CODE_BLOCK_TAB: {borderRadius: 'borderRadius0'}, | ||
CODE_BLOCK_WRAPPER: {width: 'size50'}, | ||
CODE_BLOCK: {width: 'size50'}, | ||
}} | ||
> | ||
{children} | ||
</CustomizationProvider> | ||
); | ||
|
||
const CustomizationMyWrapper: React.FC = ({children}) => ( | ||
<CustomizationProvider | ||
baseTheme="default" | ||
theme={TestTheme} | ||
elements={{ | ||
MY_CODE_BLOCK_CONTENT: {width: 'size50'}, | ||
MY_CODE_BLOCK_COPY_BUTTON: {backgroundColor: 'colorBackgroundErrorWeakest'}, | ||
MY_CODE_BLOCK_EXTERNAL_LINK: {backgroundColor: 'colorBackgroundErrorWeakest'}, | ||
MY_CODE_BLOCK_HEADER: {borderTopRightRadius: 'borderRadius30'}, | ||
MY_CODE_BLOCK_TAB_LIST: {columnGap: 'space0'}, | ||
MY_CODE_BLOCK_TAB_PANEL: {borderBottomRightRadius: 'borderRadius30'}, | ||
MY_CODE_BLOCK_TAB: {borderRadius: 'borderRadius0'}, | ||
MY_CODE_BLOCK_WRAPPER: {width: 'size50'}, | ||
MY_CODE_BLOCK: {width: 'size50'}, | ||
}} | ||
> | ||
{children} | ||
</CustomizationProvider> | ||
); | ||
|
||
describe('Customization', () => { | ||
describe('CodeBlock', () => { | ||
it('should set a default element data attribute', () => { | ||
render( | ||
<CodeBlockWrapper> | ||
<CodeBlockHeader>My code block</CodeBlockHeader> | ||
<CodeBlockTabList> | ||
<CodeBlockTab>JavaScript</CodeBlockTab> | ||
</CodeBlockTabList> | ||
<CodeBlockTabPanel> | ||
<CodeBlock language="javascript" code={jsCode} data-testid="code-block" externalLink="www.google.com" /> | ||
</CodeBlockTabPanel> | ||
</CodeBlockWrapper>, | ||
{ | ||
wrapper: CustomizationWrapper, | ||
} | ||
); | ||
|
||
const codeBlock = screen.getByTestId('code-block'); | ||
const content = codeBlock.querySelector('pre')?.parentElement; | ||
const heading = screen.getByRole('heading', {name: 'My code block'}); | ||
const wrapper = heading.parentElement; | ||
const tabList = screen.getByRole('tablist'); | ||
const tab = screen.getByRole('tab', {name: 'JavaScript'}); | ||
const tabPanel = codeBlock.parentElement; | ||
const copyButton = screen.getByRole('button', {name: 'Copy code block'}); | ||
const externalLink = screen.getByRole('link', {name: 'Open code block in new page'}); | ||
|
||
expect(wrapper?.getAttribute('data-paste-element')).toBe('CODE_BLOCK_WRAPPER'); | ||
expect(content?.getAttribute('data-paste-element')).toBe('CODE_BLOCK_CONTENT'); | ||
expect(tabList.getAttribute('data-paste-element')).toBe('CODE_BLOCK_TAB_LIST'); | ||
expect(tab.getAttribute('data-paste-element')).toBe('CODE_BLOCK_TAB'); | ||
expect(tabPanel?.getAttribute('data-paste-element')).toBe('CODE_BLOCK_TAB_PANEL'); | ||
expect(codeBlock.getAttribute('data-paste-element')).toBe('CODE_BLOCK'); | ||
expect(heading.getAttribute('data-paste-element')).toBe('CODE_BLOCK_HEADER'); | ||
expect(copyButton.getAttribute('data-paste-element')).toBe('CODE_BLOCK_COPY_BUTTON'); | ||
expect(externalLink.getAttribute('data-paste-element')).toBe('CODE_BLOCK_EXTERNAL_LINK'); | ||
}); | ||
|
||
it('should set a custom element data attribute', () => { | ||
render( | ||
<CodeBlockWrapper data-testid="wrapper" element="MY_CODE_BLOCK_WRAPPER"> | ||
<CodeBlockHeader element="MY_CODE_BLOCK_HEADER">My code block</CodeBlockHeader> | ||
<CodeBlockTabList element="MY_CODE_BLOCK_TAB_LIST"> | ||
<CodeBlockTab element="MY_CODE_BLOCK_TAB">JavaScript</CodeBlockTab> | ||
</CodeBlockTabList> | ||
<CodeBlockTabPanel element="MY_CODE_BLOCK_TAB_PANEL"> | ||
<CodeBlock | ||
language="javascript" | ||
code={jsCode} | ||
data-testid="code-block" | ||
externalLink="www.google.com" | ||
element="MY_CODE_BLOCK" | ||
/> | ||
</CodeBlockTabPanel> | ||
</CodeBlockWrapper>, | ||
{ | ||
wrapper: CustomizationMyWrapper, | ||
} | ||
); | ||
|
||
const codeBlock = screen.getByTestId('code-block'); | ||
const content = codeBlock.querySelector('pre')?.parentElement; | ||
const heading = screen.getByRole('heading', {name: 'My code block'}); | ||
const wrapper = heading.parentElement; | ||
const tabList = screen.getByRole('tablist'); | ||
const tab = screen.getByRole('tab', {name: 'JavaScript'}); | ||
const tabPanel = codeBlock.parentElement; | ||
const copyButton = screen.getByRole('button', {name: 'Copy code block'}); | ||
const externalLink = screen.getByRole('link', {name: 'Open code block in new page'}); | ||
|
||
expect(wrapper?.getAttribute('data-paste-element')).toBe('MY_CODE_BLOCK_WRAPPER'); | ||
expect(content?.getAttribute('data-paste-element')).toBe('MY_CODE_BLOCK_CONTENT'); | ||
expect(tabList.getAttribute('data-paste-element')).toBe('MY_CODE_BLOCK_TAB_LIST'); | ||
expect(tab.getAttribute('data-paste-element')).toBe('MY_CODE_BLOCK_TAB'); | ||
expect(tabPanel?.getAttribute('data-paste-element')).toBe('MY_CODE_BLOCK_TAB_PANEL'); | ||
expect(codeBlock.getAttribute('data-paste-element')).toBe('MY_CODE_BLOCK'); | ||
expect(heading.getAttribute('data-paste-element')).toBe('MY_CODE_BLOCK_HEADER'); | ||
expect(copyButton.getAttribute('data-paste-element')).toBe('MY_CODE_BLOCK_COPY_BUTTON'); | ||
expect(externalLink.getAttribute('data-paste-element')).toBe('MY_CODE_BLOCK_EXTERNAL_LINK'); | ||
}); | ||
|
||
it('should add custom styles to the component', () => { | ||
render( | ||
<CodeBlockWrapper> | ||
<CodeBlockHeader>My code block</CodeBlockHeader> | ||
<CodeBlockTabList> | ||
<CodeBlockTab>JavaScript</CodeBlockTab> | ||
</CodeBlockTabList> | ||
<CodeBlockTabPanel> | ||
<CodeBlock language="javascript" code={jsCode} data-testid="code-block" externalLink="www.google.com" /> | ||
</CodeBlockTabPanel> | ||
</CodeBlockWrapper>, | ||
{ | ||
wrapper: CustomizationWrapper, | ||
} | ||
); | ||
|
||
const codeBlock = screen.getByTestId('code-block'); | ||
const content = codeBlock.querySelector('pre')?.parentElement; | ||
const heading = screen.getByRole('heading', {name: 'My code block'}); | ||
const wrapper = heading.parentElement; | ||
const tabList = screen.getByRole('tablist'); | ||
const tab = screen.getByRole('tab', {name: 'JavaScript'}); | ||
const tabPanel = codeBlock.parentElement; | ||
const copyButton = screen.getByRole('button', {name: 'Copy code block'}); | ||
const externalLink = screen.getByRole('link', {name: 'Open code block in new page'}); | ||
|
||
expect(codeBlock).toHaveStyleRule('width', '31.5rem'); | ||
expect(content).toHaveStyleRule('width', '31.5rem'); | ||
expect(wrapper).toHaveStyleRule('width', '31.5rem'); | ||
expect(heading).toHaveStyleRule('border-top-right-radius', '8px'); | ||
expect(tabList).toHaveStyleRule('column-gap', '0'); | ||
expect(tab).toHaveStyleRule('border-radius', '0'); | ||
expect(tabPanel).toHaveStyleRule('border-bottom-right-radius', '8px'); | ||
expect(copyButton).toHaveStyleRule('background-color', 'rgb(254, 236, 236)'); | ||
expect(externalLink).toHaveStyleRule('background-color', 'rgb(254, 236, 236)'); | ||
}); | ||
|
||
it('should set custom styles with custom element names', () => { | ||
render( | ||
<CodeBlockWrapper data-testid="wrapper" element="MY_CODE_BLOCK_WRAPPER"> | ||
<CodeBlockHeader element="MY_CODE_BLOCK_HEADER">My code block</CodeBlockHeader> | ||
<CodeBlockTabList element="MY_CODE_BLOCK_TAB_LIST"> | ||
<CodeBlockTab element="MY_CODE_BLOCK_TAB">JavaScript</CodeBlockTab> | ||
</CodeBlockTabList> | ||
<CodeBlockTabPanel element="MY_CODE_BLOCK_TAB_PANEL"> | ||
<CodeBlock | ||
language="javascript" | ||
code={jsCode} | ||
data-testid="code-block" | ||
externalLink="www.google.com" | ||
element="MY_CODE_BLOCK" | ||
/> | ||
</CodeBlockTabPanel> | ||
</CodeBlockWrapper>, | ||
{ | ||
wrapper: CustomizationMyWrapper, | ||
} | ||
); | ||
|
||
const codeBlock = screen.getByTestId('code-block'); | ||
const content = codeBlock.querySelector('pre')?.parentElement; | ||
const heading = screen.getByRole('heading', {name: 'My code block'}); | ||
const wrapper = heading.parentElement; | ||
const tabList = screen.getByRole('tablist'); | ||
const tab = screen.getByRole('tab', {name: 'JavaScript'}); | ||
const tabPanel = codeBlock.parentElement; | ||
const copyButton = screen.getByRole('button', {name: 'Copy code block'}); | ||
const externalLink = screen.getByRole('link', {name: 'Open code block in new page'}); | ||
|
||
expect(codeBlock).toHaveStyleRule('width', '31.5rem'); | ||
expect(content).toHaveStyleRule('width', '31.5rem'); | ||
expect(wrapper).toHaveStyleRule('width', '31.5rem'); | ||
expect(heading).toHaveStyleRule('border-top-right-radius', '8px'); | ||
expect(tabList).toHaveStyleRule('column-gap', '0'); | ||
expect(tab).toHaveStyleRule('border-radius', '0'); | ||
expect(tabPanel).toHaveStyleRule('border-bottom-right-radius', '8px'); | ||
expect(copyButton).toHaveStyleRule('background-color', 'rgb(254, 236, 236)'); | ||
expect(externalLink).toHaveStyleRule('background-color', 'rgb(254, 236, 236)'); | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.