Skip to content
This repository was archived by the owner on Sep 29, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 1 addition & 1 deletion .prettierrc
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
{
"files": "snippets/snippets/**/*.ts",
"options": {
"printWidth": 45
"printWidth": 49
}
}
]
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# [TypeScript for Beginner Programmers](https://ts.chibicode.com/)
# TypeScript for Beginner Programmers

## License & Credits

Expand Down
5 changes: 1 addition & 4 deletions snippets/snippets/generics/cupt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,5 @@ function createState() {
state = x
}

return {
getState,
setState
}
return { getState, setState }
}
15 changes: 15 additions & 0 deletions snippets/snippets/generics/gkgi.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
function createState() {
// Change to string
let state: string

function getState() {
return state
}

// Accepts a string
function setState(x: string) {
state = x
}

return { getState, setState }
}
14 changes: 14 additions & 0 deletions snippets/snippets/generics/nnyl.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
function createState() {
let state: number

function getState() {
return state
}

// setState() expects a number
function setState(x: number) {
state = x
}

return { getState, setState }
}
20 changes: 20 additions & 0 deletions snippets/snippets/generics/osaa.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
function createState() {
// Change to string
let state: string

function getState() {
return state
}

// Accepts a string
function setState(x: string) {
state = x
}

return { getState, setState }
}

const { getState, setState } = createState()

setState('foo')
console.log(getState())
5 changes: 5 additions & 0 deletions snippets/snippets/generics/stkh.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const { getState, setState } = createState()

// What happens if we use a string instead?
setState('foo')
console.log(getState())
5 changes: 1 addition & 4 deletions snippets/snippets/generics/udpv.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,7 @@ function createState() {
state = x
}

return {
getState,
setState
}
return { getState, setState }
}

const { getState, setState } = createState()
Expand Down
4 changes: 4 additions & 0 deletions snippets/snippets/generics/xeax.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
const { getState, setState } = createState()

setState('foo')
console.log(getState())
18 changes: 18 additions & 0 deletions snippets/snippets/generics/zhql.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
function createState() {
let state: number

function getState() {
return state
}

function setState(x: number) {
state = x
}

return { getState, setState }
}

const { getState, setState } = createState()

setState('foo')
console.log(getState())
71 changes: 56 additions & 15 deletions src/components/CodeBlock.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { useState } from 'react'
import useTheme from 'src/hooks/useTheme'
import Caption from 'src/components/Caption'
import ButtonWithTouchActiveStates from 'src/components/ButtonWithTouchActiveStates'
import Emoji from 'src/components/Emoji'
import RunButtonText from 'src/components/RunButtonText'
import PrismHighlight, { defaultProps } from 'prism-react-renderer'
import theme from 'src/lib/prismTheme'

Expand All @@ -15,7 +15,10 @@ const CodeBlock = ({
pointToRunButton,
defaultResultVisible,
caption,
noHighlight
noHighlight,
compile,
shouldHighlightResult,
resultError
}: {
snippet: string
shouldHighlight?: (lineNumber: number, tokenNumber: number) => boolean
Expand All @@ -24,6 +27,9 @@ const CodeBlock = ({
defaultResultVisible?: boolean
caption?: React.ReactNode
noHighlight?: boolean
compile?: boolean
shouldHighlightResult?: (lineNumber: number, tokenNumber: number) => boolean
resultError?: boolean
}) => {
const [resultVisible, setResultVisible] = useState(defaultResultVisible)
const { radii, colors, ns, maxWidths, spaces, fontSizes } = useTheme()
Expand Down Expand Up @@ -55,18 +61,20 @@ const CodeBlock = ({
<pre
css={[
css`
padding: ${spaces(0.75)} ${spaces(0.75)};
padding: ${spaces(0.75)} ${spaces(0.5)};
line-height: 1.45;

border: 2px solid ${colors('lightBrown')};
background-color: ${colors('lightPink1')};
margin-top: ${caption ? 0 : spaces(1.75)};
margin-bottom: ${result ? 0 : spaces(1.75)};
font-size: ${fontSizes(0.8)};
margin-left: ${spaces('-0.5')};
margin-right: ${spaces('-0.5')};

${ns} {
font-size: ${fontSizes(0.85)};
padding: ${spaces(1)} ${spaces(1.25)};
padding: ${spaces(1)} ${spaces(1)};
margin-left: 0;
margin-right: 0;
}
Expand Down Expand Up @@ -111,11 +119,30 @@ const CodeBlock = ({
css`
font-style: normal !important;
`,
!!shouldHighlight &&
shouldHighlight(i, key) &&
((!!shouldHighlight &&
!resultVisible &&
shouldHighlight(i, key)) ||
(!!shouldHighlightResult &&
resultVisible &&
shouldHighlightResult(i, key))) &&
css`
background: ${colors('white')};
border-bottom: 2px solid ${colors('red')};
background: ${shouldHighlightResult &&
resultVisible &&
resultError
? colors('white')
: colors('yellowHighlight')};
border-bottom: ${shouldHighlightResult &&
resultVisible &&
resultError
? 'none'
: `3px solid ${colors('darkOrange')}`};
text-decoration: ${shouldHighlightResult &&
resultVisible &&
resultError
? 'underline'
: 'none'};
text-decoration-style: wavy;
text-decoration-color: ${colors('red')};
`
]}
/>
Expand All @@ -134,14 +161,20 @@ const CodeBlock = ({
css={css`
max-width: ${maxWidths('sm')};
margin-bottom: ${spaces(1.75)};
margin-left: ${spaces(0)};
margin-right: ${spaces(0)};
margin-left: ${spaces('-0.5')};
margin-right: ${spaces('-0.5')};

${ns} {
margin-left: ${spaces(0)};
margin-right: ${spaces(0)};
}
`}
>
{resultVisible ? (
<div
css={[
css`
line-height: 1.45;
border-top-left-radius: 0;
border-top-right-radius: 0;
border-bottom-left-radius: ${radii(0.5)};
Expand All @@ -150,17 +183,26 @@ const CodeBlock = ({
border-left: 2px solid ${colors('lightBrown')};
border-bottom: 2px solid ${colors('lightBrown')};
border-right: 2px solid ${colors('lightBrown')};
padding: ${spaces(0.5)} ${spaces(0.75)};
padding: ${spaces(0.5)} ${spaces(0.5)};
font-size: ${fontSizes(0.8)};

${ns} {
padding: ${spaces(0.75)} ${spaces(1.25)};
padding: ${spaces(0.75)} ${spaces(1)};
font-size: ${fontSizes(0.85)};
}
`
]}
>
<code>{result}</code>
<code
css={
resultError &&
css`
color: ${colors('red')};
`
}
>
{result}
</code>
</div>
) : (
<div
Expand All @@ -178,7 +220,6 @@ const CodeBlock = ({
line-height: 1.1rem;
border: none;
margin-bottom: 0;
font-weight: bold;
font-size: ${fontSizes(0.85)};
background: ${colors('white')};
border: 2px solid ${colors('lightBrown')};
Expand All @@ -204,7 +245,7 @@ const CodeBlock = ({
`
]}
>
Run <Emoji type="run" />
<RunButtonText compile={compile} />
</ButtonWithTouchActiveStates>
{pointToRunButton && (
<>
Expand Down
4 changes: 2 additions & 2 deletions src/components/RunButtonText.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@
import { css, jsx } from '@emotion/core'
import Emoji from 'src/components/Emoji'

const RunButtonText = () => (
const RunButtonText = ({ compile }: { compile?: boolean }) => (
<span
css={css`
font-weight: bold;
`}
>
Run <Emoji type="run" />
{compile ? 'Compile' : 'Run'} <Emoji type="run" />
</span>
)

Expand Down
Loading