跳转到正文

变体

区块变体API 允许你为现有区块注册自定义变体。

假设有一个区块,需要根据简单选择字段的值提供两种不同的变体。

JSON
{
  "$schema": "https://blockstudio.dev/schema/block",
  "name": "blockstudio/variations",
  "title": "Native Variation",
  "description": "Native Blockstudio Variation block.",
  "icon": "star-filled",
  "variations": [
    {
      "name": "variation-2",
      "title": "Native Variation 2",
      "description": "Native Blockstudio Variation block 2.",
      "attributes": {
        "select": {
          "value": "variation-2"
        }
      }
    }
  ],
  "blockstudio": {
    "attributes": [
      {
        "id": "select",
        "type": "select",
        "label": "Select",
        "options": [
          {
            "value": "variation-1",
            "label": "Variation 1"
          },
          {
            "value": "variation-2",
            "label": "Variation 2"
          }
        ],
        "default": {
          "value": "variation-1"
        }
      }
    ]
  }
}

以上内容会在插入器中创建三个区块:主区块和两个变体。

隐藏属性

属性会自动在侧边栏中渲染相应的输入控件。由于变体区块依赖特定属性值,你可能希望在侧边栏中隐藏这些字段。为此,可以使用 hidden 选项。

JSON
{
  "blockstudio": {
    "attributes": [
      {
        "id": "select",
        "type": "select",
        "label": "Select",
        "hidden": true,
        "options": [
          {
            "value": "variation-1",
            "label": "Variation 1"
          },
          {
            "value": "variation-2",
            "label": "Variation 2"
          }
        ]
      }
    ]
  }
}