See for instance Component Wise Dirichlet BC - #2 by dokken or Component-wise Dirichlet BC — FEniCSx tutorial