Skip to content

feat: more enhanced code blocks (follow up for #3235) #3258

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
16 changes: 10 additions & 6 deletions src/api/sfc-script-setup.md
Original file line number Diff line number Diff line change
Expand Up @@ -298,16 +298,20 @@ function inc() {
:::warning
If you have a `default` value for `defineModel` prop and you don't provide any value for this prop from the parent component, it can cause a de-synchronization between parent and child components. In the example below, the parent's `myRef` is undefined, but the child's `model` is 1:

```js
// child component:
```vue [Child.vue]
<script setup>
const model = defineModel({ default: 1 })
</script>
```

// parent component:
```vue [Parent.vue]
<script setup>
const myRef = ref()
```
</script>

```html
<Child v-model="myRef"></Child>
<template>
<Child v-model="myRef"></Child>
</template>
```

:::
Expand Down
3 changes: 1 addition & 2 deletions src/guide/built-ins/transition.md
Original file line number Diff line number Diff line change
Expand Up @@ -469,8 +469,7 @@ Here's a demo using the [GSAP library](https://gsap.com/) to perform the animati

Transitions can be reused through Vue's component system. To create a reusable transition, we can create a component that wraps the `<Transition>` component and passes down the slot content:

```vue{5}
<!-- MyTransition.vue -->
```vue{6} [MyTransition.vue]
<script>
// JavaScript hooks logic...
</script>
Expand Down
5 changes: 2 additions & 3 deletions src/guide/components/provide-inject.md
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,7 @@ provide('read-only-count', readonly(count))

In order to make injections reactively linked to the provider, we need to provide a computed property using the [computed()](/api/reactivity-core#computed) function:

```js{10}
```js{12}
import { computed } from 'vue'

export default {
Expand Down Expand Up @@ -327,8 +327,7 @@ So far, we have been using string injection keys in the examples. If you are wor

It's recommended to export the Symbols in a dedicated file:

```js
// keys.js
```js [keys.js]
export const myInjectionKey = Symbol()
```

Expand Down
55 changes: 23 additions & 32 deletions src/guide/components/v-model.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@

Starting in Vue 3.4, the recommended approach to achieve this is using the [`defineModel()`](/api/sfc-script-setup#definemodel) macro:

```vue
<!-- Child.vue -->
```vue [Child.vue]
<script setup>
const model = defineModel()

Expand All @@ -30,8 +29,7 @@ function update() {

The parent can then bind a value with `v-model`:

```vue-html
<!-- Parent.vue -->
```vue-html [Parent.vue]
<Child v-model="countModel" />
```

Expand Down Expand Up @@ -63,8 +61,7 @@ const model = defineModel()

This is how you would implement the same child component shown above prior to 3.4:

```vue
<!-- Child.vue -->
```vue [Child.vue]
<script setup>
const props = defineProps(['modelValue'])
const emit = defineEmits(['update:modelValue'])
Expand All @@ -80,8 +77,7 @@ const emit = defineEmits(['update:modelValue'])

Then, `v-model="foo"` in the parent component will be compiled to:

```vue-html
<!-- Parent.vue -->
```vue-html [Parent.vue]
<Child
:modelValue="foo"
@update:modelValue="$event => (foo = $event)"
Expand All @@ -103,20 +99,20 @@ const model = defineModel({ default: 0 })
:::warning
If you have a `default` value for `defineModel` prop and you don't provide any value for this prop from the parent component, it can cause a de-synchronization between parent and child components. In the example below, the parent's `myRef` is undefined, but the child's `model` is 1:

**Child component:**

```js
```vue [Child.vue]
<script setup>
const model = defineModel({ default: 1 })
</script>
```

**Parent component:**

```js
```vue [Parent.vue]
<script setup>
const myRef = ref()
```
</script>

```html
<Child v-model="myRef"></Child>
<template>
<Child v-model="myRef"></Child>
</template>
```

:::
Expand Down Expand Up @@ -156,8 +152,7 @@ For this to actually work though, the `<CustomInput>` component must do two thin

Here's that in action:

```vue
<!-- CustomInput.vue -->
```vue [CustomInput.vue]
<script>
export default {
props: ['modelValue'],
Expand All @@ -183,8 +178,7 @@ Now `v-model` should work perfectly with this component:

Another way of implementing `v-model` within this component is to use a writable `computed` property with both a getter and a setter. The `get` method should return the `modelValue` property and the `set` method should emit the corresponding event:

```vue
<!-- CustomInput.vue -->
```vue [CustomInput.vue]
<script>
export default {
props: ['modelValue'],
Expand Down Expand Up @@ -221,8 +215,7 @@ export default {

In the child component, we can support the corresponding argument by passing a string to `defineModel()` as its first argument:

```vue
<!-- MyComponent.vue -->
```vue [MyComponent.vue]
<script setup>
const title = defineModel('title')
</script>
Expand All @@ -243,8 +236,7 @@ const title = defineModel('title', { required: true })
<details>
<summary>Pre 3.4 Usage</summary>

```vue
<!-- MyComponent.vue -->
```vue [MyComponent.vue]
<script setup>
defineProps({
title: {
Expand All @@ -271,8 +263,7 @@ defineEmits(['update:title'])

In this case, instead of the default `modelValue` prop and `update:modelValue` event, the child component should expect a `title` prop and emit an `update:title` event to update the parent value:

```vue
<!-- MyComponent.vue -->
```vue [MyComponent.vue]
<script>
export default {
props: ['title'],
Expand Down Expand Up @@ -412,7 +403,7 @@ console.log(modifiers) // { capitalize: true }

To conditionally adjust how the value should be read / written based on modifiers, we can pass `get` and `set` options to `defineModel()`. These two options receive the value on get / set of the model ref and should return a transformed value. This is how we can use the `set` option to implement the `capitalize` modifier:

```vue{6-8}
```vue{4-6}
<script setup>
const [model, modifiers] = defineModel({
set(value) {
Expand Down Expand Up @@ -577,10 +568,10 @@ console.log(lastNameModifiers) // { uppercase: true }
```vue{5,6,10,11}
<script setup>
const props = defineProps({
firstName: String,
lastName: String,
firstNameModifiers: { default: () => ({}) },
lastNameModifiers: { default: () => ({}) }
firstName: String,
lastName: String,
firstNameModifiers: { default: () => ({}) },
lastNameModifiers: { default: () => ({}) }
})
defineEmits(['update:firstName', 'update:lastName'])

Expand Down
23 changes: 9 additions & 14 deletions src/guide/essentials/component-basics.md
Original file line number Diff line number Diff line change
Expand Up @@ -188,8 +188,7 @@ Props are custom attributes you can register on a component. To pass a title to

<div class="options-api">

```vue
<!-- BlogPost.vue -->
```vue [BlogPost.vue]
<script>
export default {
props: ['title']
Expand All @@ -206,8 +205,7 @@ When a value is passed to a prop attribute, it becomes a property on that compon
</div>
<div class="composition-api">

```vue
<!-- BlogPost.vue -->
```vue [BlogPost.vue]
<script setup>
defineProps(['title'])
</script>
Expand Down Expand Up @@ -352,8 +350,8 @@ Which can be used in the template to control the font size of all blog posts:

Now let's add a button to the `<BlogPost>` component's template:

```vue{5}
<!-- BlogPost.vue, omitting <script> -->
```vue{5} [BlogPost.vue]
<!-- omitting <script> -->
<template>
<div class="blog-post">
<h4>{{ title }}</h4>
Expand All @@ -373,8 +371,8 @@ The button doesn't do anything yet - we want clicking the button to communicate

Then the child component can emit an event on itself by calling the built-in [**`$emit`** method](/api/component-instance#emit), passing the name of the event:

```vue{5}
<!-- BlogPost.vue, omitting <script> -->
```vue{5} [BlogPost.vue]
<!-- omitting <script> -->
<template>
<div class="blog-post">
<h4>{{ title }}</h4>
Expand All @@ -400,8 +398,7 @@ We can optionally declare emitted events using the <span class="options-api">[`e

<div class="options-api">

```vue{5}
<!-- BlogPost.vue -->
```vue{4} [BlogPost.vue]
<script>
export default {
props: ['title'],
Expand All @@ -413,8 +410,7 @@ export default {
</div>
<div class="composition-api">

```vue{4}
<!-- BlogPost.vue -->
```vue{3} [BlogPost.vue]
<script setup>
defineProps(['title'])
defineEmits(['enlarge-text'])
Expand Down Expand Up @@ -472,8 +468,7 @@ Something bad happened.

This can be achieved using Vue's custom `<slot>` element:

```vue{5}
<!-- AlertBox.vue -->
```vue{4} [AlertBox.vue]
<template>
<div class="alert-box">
<strong>This is an Error for Demo Purposes</strong>
Expand Down
22 changes: 7 additions & 15 deletions src/guide/extras/web-components.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,7 @@ export default {

#### Example Vue CLI Config {#example-vue-cli-config}

```js
// vue.config.js
```js [vue.config.js]
module.exports = {
chainWebpack: (config) => {
config.module
Expand Down Expand Up @@ -218,8 +217,7 @@ If the custom elements will be used in an application that is also using Vue, yo

It is recommended to export the individual element constructors to give your users the flexibility to import them on-demand and register them with desired tag names. You can also export a convenience function to automatically register all elements. Here's an example entry point of a Vue custom element library:

```js
// elements.js
```js [elements.js]

import { defineCustomElement } from 'vue'
import Foo from './MyFoo.ce.vue'
Expand Down Expand Up @@ -311,9 +309,7 @@ This approach is one possible way to do it, but it may vary depending on the fra

Suppose we have a custom element with some JS properties and events defined, and it is shipped in a library called `some-lib`:

```ts
// file: some-lib/src/SomeElement.ts

```ts [some-lib/src/SomeElement.ts]
// Define a class with typed JS properties.
export class SomeElement extends HTMLElement {
foo: number = 123
Expand Down Expand Up @@ -351,9 +347,7 @@ The implementation details have been omitted, but the important part is that we

Let's create a type helper for easily registering custom element type definitions in Vue:

```ts
// file: some-lib/src/DefineCustomElement.ts

```ts [some-lib/src/DefineCustomElement.ts]
// We can re-use this type helper per each element we need to define.
type DefineCustomElement<
ElementType extends HTMLElement,
Expand Down Expand Up @@ -394,9 +388,7 @@ We marked `$props` and `$emit` as deprecated so that when we get a `ref` to a cu

Using the type helper we can now select the JS properties that should be exposed for type checking in Vue templates:

```ts
// file: some-lib/src/SomeElement.vue.ts

```ts [some-lib/src/SomeElement.vue.ts]
import {
SomeElement,
SomeElementAttributes,
Expand All @@ -419,7 +411,7 @@ declare module 'vue' {

Suppose that `some-lib` builds its source TypeScript files into a `dist/` folder. A user of `some-lib` can then import `SomeElement` and use it in a Vue SFC like so:

```vue
```vue [SomeElementImpl.vue]
<script setup lang="ts">
// This will create and register the element with the browser.
import 'some-lib/dist/SomeElement.js'
Expand Down Expand Up @@ -465,7 +457,7 @@ onMounted(() => {

If an element does not have type definitions, the types of the properties and events can be defined in a more manual fashion:

```vue
```vue [SomeElementImpl.vue]
<script setup lang="ts">
// Suppose that `some-lib` is plain JS without type definitions, and TypeScript
// cannot infer the types:
Expand Down
Loading